jQuery 未调用 AJAX 成功函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18029042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:24:24  来源:igfitidea点击:

AJAX success function not being called

javascriptjqueryruby-on-railsajax

提问by user2495030

I am trying to get the success function from an AJAX call to fire. I know it is properly working because I am hitting my own API and I can see it is properly hitting the URL and the server is outputting a HTTP 200.

我正在尝试从 AJAX 调用中获取成功函数以进行触发。我知道它可以正常工作,因为我正在访问自己的 API,并且可以看到它正确地访问了 URL,并且服务器正在输出 HTTP 200。

I figured it was because the server is outputting json, so I tried to account for that in the AJAX call, but still the success function will not work. Here is my code

我想这是因为服务器正在输出 json,所以我试图在 AJAX 调用中考虑到这一点,但成功功能仍然不起作用。这是我的代码

ajax

阿贾克斯

$.ajax('http://localhost:3000/api/users/show/:id', {
  type: 'GET',
  dataType: 'json',
  contentType: "application/json",
  data: {
    id: 1
  },
  success: function(response) {
    return alert("Hey");
  }
});

api method

api方法

class UsersController < ApplicationController
    respond_to :json

    def show
        respond_with User.find(params[:id])
    end

end

server logs

服务器日志

Started GET "/api/users/show/:id?id=1" for 127.0.0.1 at 2013-08-02 20:36:42 -0700
Processing by MainController#index as JSON
  Parameters: {"id"=>"1", "path"=>"api/users/show/:id", "main"=>{}}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" =  LIMIT 1  [["id", 1]]
  Rendered main/index.html.erb within layouts/application (0.6ms)
Completed 200 OK in 146ms (Views: 144.3ms | ActiveRecord: 0.5ms)
[2013-08-02 20:36:42] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

回答by Ashis Kumar

This happened with me also a long back, i solved this by changing the dataType to text, and manually converting that to json object through eval.

这也发生在我很久以前,我通过将数据类型更改为文本并通过 eval 手动将其转换为 json 对象来解决此问题。

$.ajax('http://localhost:3000/api/users/show/:id', {
  type: 'GET',
  dataType: 'text',
  contentType: "application/json",
  data: {
    id: 1
  },
  success: function(response) {
    response = eval(response);
    return alert("Hey");
  }
});

May this work for you.

愿这对你有用。

回答by SemanticZen

I would add a complete function and check the text status. That should give the information you need to solve the problem.

我会添加一个完整的功能并检查文本状态。这应该提供解决问题所需的信息。

complete: function(response, textStatus) {
    return alert("Hey: " + textStatus);
  }

回答by David

I think the problem is what I have been experiencing, and I think I have the answer. It looks like your call is retrieving an HTML view, as I infer from "Rendered main/index.html.erb within layouts/application", though I'm not familiar with whatever API stack you're using.

我认为问题是我一直在经历的,我想我有答案。看起来您的调用正在检索 HTML 视图,正如我从“在布局/应用程序中呈现的 main/index.html.erb”推断的那样,尽管我不熟悉您使用的任何 API 堆栈。

My symptoms on ASP.NET MVC 5 were that complete was called, but none of error, success or timeout were called. On the response object, status was 200 and statusText was "OK", but the textStatus parameter was "parsererror".

我在 ASP.NET MVC 5 上的症状是调用了 complete,但没有调用错误、成功或超时。在响应对象上,status 为 200,statusText 为“OK”,但 textStatus 参数为“parsererror”。

The responseText is the html I was expecting, but I'd forgotten that I'd moved from retrieving json to html, so after changing datatype: 'json' to datatype: 'html', it now works.

responseText 是我期待的 html,但我忘记了我已经从检索 json 转移到 html,所以在将 datatype: 'json' 更改为 datatype: 'html' 后,它现在可以工作了。

回答by Lo?c Faure-Lacroix

It isn't apparently calling the method that you are expecting, considering this class:

考虑到这个类,它显然没有调用您期望的方法:

 class UsersController < ApplicationController

We should see something like this in your logs:

我们应该在您的日志中看到类似的内容:

Processing by UsersController#show as JSON

But your logs are showing this:

但是您的日志显示了这一点:

Processing by MainController#index as JSON

My guess is that your routes are wrong. Check the routes and why it's not calling the UsersController#showmethod. Also just to be certain, with the browser (chrome, firefox), You should be able to inspect the response of the request to make sure it is actually receiving json or html.

我的猜测是你的路线是错误的。检查路由以及为什么它不调用该UsersController#show方法。同样可以肯定的是,使用浏览器(chrome、firefox),您应该能够检查请求的响应以确保它实际接收的是 json 或 html。

Since it is trying to render main.html.erb. I'm not surprised the dataType: "json"isn't working. But it should work if you were actually returning valid json. But your rails logs are showing us that you are probably returning html.

由于它正在尝试渲染main.html.erb. 我并不惊讶dataType: "json"它不起作用。但如果您实际返回有效的 json,它应该可以工作。但是您的 rails 日志向我们表明您可能正在返回 html。