Alex Neigher
Alex Neigher

Reputation: 907

jQuery AJAX call not returning data

I feel a little silly here. My ajax call is running the error: function every time. I know that the data is coming back as JSON, but i've done the datatype as jsonp to allow for cross origin stuff. I don't think I can do anything differently, unless I'm forgetting something obvious. Please- whats wrong with this:

function sartleApi(type,endpoint,object,callback){


 $.ajax({
    contentType: "application/json",
    dataType: 'jsonp',
    type:type,
    data:object,
    url:"http://dev.sartle.com/includes/ajax_reviewcomment.php?rid=1178",
    success:function(data){
        callback(data);
    },

    error: function (xhr, textStatus, errorThrown) {
        alert(xhr.statusText);
        alert(xhr.responseText);
        alert(xhr.status);
        alert(errorThrown);
    }
});

}

Upvotes: 1

Views: 2099

Answers (4)

Blender
Blender

Reputation: 298106

Your website doesn't support JSONP.

JSONP is just a fancy way of passing a JSON object to a global callback function via a <script> tag. It circumvents cross-origin restrictions by not sending an AJAX request in the first place, but instead creating a <script> tag.

A JSON response looks like this:

{"foo": "bar"}

But a JSONP response is:

some_callback({"foo": "bar"})

That PHP script doesn't wrap the JSON response in a callback function (whose name is usually specified via the callback GET parameter), so you simply can't make a JSONP request. The request will succeed, but the global callback function will not be called, so you will not be able to use the JSON.

Upvotes: 2

user1236048
user1236048

Reputation: 5602

  • stlll, it seems a cross-domain issue: please try this library https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js

  • as a jsonp the result is already a callback function with a javascript object argument. Make sure that the returned function by the server is implemented: The server could return my_callback({...}). You need to implement my_callback function client-side.

  • place some alerts/console.log on both success and error functions. Always make a little debug of your own before posting the issue.

  • as posted in the comment: state the return code of the ajax call.

Upvotes: 0

Alex Neigher
Alex Neigher

Reputation: 907

So, to try to circumvent the cross domain issue, using jSONP turned out to be a bad idea. I'm running these calls from a localhost, so i've changed the url in the ajax call to

url:"http://localhost/includes/ajax_reviewcomment.php?rid=1178"

I will build this url to be dynamic so that the current URL always is domain-consistent with the server, and i should be in good shape!

Upvotes: 0

Brakyo
Brakyo

Reputation: 128

in type you need put "GET" or "POST"

$.ajax({
    contentType: "application/json",
    dataType: 'jsonp',
    type:type, <<<<<<------ here 
    data:object,
    url:"http://dev.sartle.com/includes/ajax_reviewcomment.php?rid=1178",
    success:function(data){
        callback(data);
    },

error: function (xhr, textStatus, errorThrown) {
    alert(xhr.statusText);
    alert(xhr.responseText);
    alert(xhr.status);
    alert(errorThrown);
}

});

}

Upvotes: -1

Related Questions