I have following ajax call in my JavaScript code
url = 'http://news.ycombinator.com/?callback=?';
$.ajax({url:url ,async:!1,dataType:'script', complete:function(result)
{alert(JSON.stringify(result));}
});
Following is printed out in alert.
{'readyState':4, status:200, statusText:'success'}
It doesn’t have responseText.But still, in the chrome console I can see all of the return HTML data of ycombinator page.How can I access this text?
On the other hand if I change the url variable to a url which returns a valid json object like following,
urll = 'http://gdata.youtube.com/feeds/api/videos?q=basshunter&format=5&max-results=5&v=2&alt=jsonc';
$.ajax({url:urll ,async:!1, complete:function(result)
{alert(JSON.stringify(result));}
});
this returns all of the responseText as expected.
One thing to note is if I don’t point the url to a valid JSON returning url as in first case, I have to provide option dataType:’script'(or JSON).Otherwise it will throw a cross-domain request error.In second case it didn’t throw any cross-domain error even if I didn’t specify dataType.
Replace your complete callback with success. Success callback executed when ajax request completed successfully.
Also in your dataType use “json” instead of “script”
if you use “script” in dataType it will return like
"{\"key\":\"value\"}".if you use “json” in dataType it will return like
{"key":"value"}.