简体   繁体   English

jQuery AJAX在200成功响应上失败404

[英]jQuery AJAX failing with 404 on a 200 success response

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. 以下只是警报: An error occured while executing AJAX request错误,错误代码为404。但是Chrome检查器显示请求成功,并且输出是有效的JSON。

What am I doing wrong? 我究竟做错了什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM