jQuery AJAX在200次成功响应时失败了404

时间:2021-02-26 20:15:23

I have a simple API, written in Rails, which should be used with AJAX from jQuery.

我有一个用Rails编写的简单API,它应该与jQuery的AJAX一起使用。

Here is how my controller method looks like:

以下是我的控制器方法的样子:

  user = User.find_by_email_and_password(params[:email], params[:password])
  if ! user
    user = User.new
    user.email = params[:email]
    user.password = params[:password]
    user.save
  end

  respond_with user, status: 200

This is the AJAX handler:

这是AJAX处理程序:

$.ajax({
    url: Service.url + 'login.json',
    data: {
      email: 'email@server.com',
      password: 'pass'
    },
    type: 'POST',
    dataType: 'json',
    success: function(json) {
      if (json.id) {
        this.is_logged_in = true;
        this.data = json;
        $('#finilizeModal').modal('show');
        return $('#signInModal').modal('hide');
      }
    },
    error: function(xhr, status) {
      return alert('An error occured while executing AJAX request');
    },
    complete: function() {}
  });

The following just alerts: An error occured while executing AJAX request and has an error code of 404. But the Chrome Inspector shows that the request was successful and the output is a valid JSON.

以下只是警报:执行AJAX请求时发生错误,错误代码为404.但Chrome Inspector显示请求成功,输出是有效的JSON。

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


0  

Try to generate page without '.json' in rails, because: URL in rails '/somepage?email=emsil&password=pass.json', jQuery send '/somepage.json?email=email&password=pass'.

尝试在rails中生成没有'.json'的页面,因为:rails在/somepage?email=emsil&password=pass.json'中的URL,jQuery发送'/somepage.json?email=email&password=pass'。

#1


0  

Try to generate page without '.json' in rails, because: URL in rails '/somepage?email=emsil&password=pass.json', jQuery send '/somepage.json?email=email&password=pass'.

尝试在rails中生成没有'.json'的页面,因为:rails在/somepage?email=emsil&password=pass.json'中的URL,jQuery发送'/somepage.json?email=email&password=pass'。