简体   繁体   中英

AJAX success function not being called

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.

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

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

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" = $1 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

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.

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

May this work for you.

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);
  }

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.

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".

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.

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#show method. 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.

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.

我遇到了同样的问题,我通过在关闭成功函数后添加一个冒号来解决它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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