user2495030
user2495030

Reputation: 133

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

Upvotes: 13

Views: 36834

Answers (5)

Bruno Ferreira
Bruno Ferreira

Reputation: 25

I had the same problem and i solved it by adding a colon after closing the success function.

Upvotes: 0

Ashis Kumar
Ashis Kumar

Reputation: 6554

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.

Upvotes: 23

Lo&#239;c Faure-Lacroix
Lo&#239;c Faure-Lacroix

Reputation: 13610

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.

Upvotes: 0

David
David

Reputation: 1774

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.

Upvotes: 2

SemanticZen
SemanticZen

Reputation: 1151

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

Upvotes: 8

Related Questions