Reputation: 145
I tried the below code to make a HTTP GET request to get some data from a server. Looks like, the HTTP request is not at all reaching the server. Am I missing something here?
I tried with $.get(url, function(data){alert(data);}, "json").error(onHttpError)
too. But no luck.
$(document).ready(function()
{
$.getJSON(url, onHttpSuccess).error(onHttpError);
/**
*onHttpSuccess
*/
function onHttpSuccess( response )
{
alert("ASG Data Received:" + response);
}
/**
*onHttpError
*/
function onHttpError()
{
var errorMsg = "HTTP Error!!!";
$('#message').html(errorMsg);
alert(errorMsg);
}
});
Experts, please help. Thanks!
DK
Upvotes: 0
Views: 552
Reputation: 145
Yes. URL has been defined before this code and it is a valid URL. I mean, when I directly use the url in a browser and hit enter, I see, the server receiving the request and sending out the response.
Upvotes: 0
Reputation: 6378
Not sure if this is the answer, but I have run into many cases where the AJAX call is working fine, but the JSON returned from the server isn't parsed by jQuery and so the entire operation appears to fail.
Often the cause is that the server hasn't specified the content type of the response, but whatever the cause the next trouble shooting step would be to do a $.get request without specifying "json" as the return type. Try specifying "text" as the return type and see if you get a response from the server.
Sometimes I have even had luck with receiving the server response as plain text and then manually running JSON.parse() on it in the success function.
Upvotes: 1