13

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
user2495030
  • 133
  • 1
  • 1
  • 5
  • 1
    possible duplicate of [What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?](http://stackoverflow.com/questions/7082364/what-does-warn-could-not-determine-content-length-of-response-body-mean-and-h) – Raptor Aug 03 '13 at 03:45
  • I dont think that has to do with an AJAX call. The answer says that warning is safe to ignore. – user2495030 Aug 03 '13 at 03:48
  • 1
    Do you know what [`dataType`](http://api.jquery.com/jQuery.ajax/) really means? Have you tried adding an `error` callback? – mu is too short Aug 03 '13 at 03:48
  • GET's shouldn't have a Content-Type – CBIII Aug 03 '13 at 03:49
  • I was looking at dataType and I thought thats what I wanted. I am expecting back a JSON object of the `User` model. I also saw that as a solution after searching online. But it didnt happen to work in my case. – user2495030 Aug 03 '13 at 03:57
  • Is the page that makes the requests served from the same domain (and part) you make the Ajax request to? If not, then you are subject to the same origin policy. – Felix Kling Aug 03 '13 at 10:55

5 Answers5

23

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.

Ashis Kumar
  • 6,494
  • 2
  • 21
  • 36
8

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);
  }
SemanticZen
  • 1,141
  • 14
  • 21
2

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.

David
  • 1,754
  • 23
  • 35
0

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.

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
0

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