Reputation: 830
I've been experiencing this weird problem when I want to retrieve data from an URL.
$.ajax({
url: 'URLHERE',
dataType: 'html',
success: function(data) { //function 2
var xml = $.parseXML(data)
$(xml).find('StopLocation').each(function() //function 3
{
var name = $(this).attr('name');
var x = $(this).attr('x');
alert(name);
alert(x);
}); //function 3 end } //function 2 end }); //ajax end
This does work in Dreamweaver, but not in browser. I've been reading that it could be because AJAX doesn't work on cross domain on browsers. Is this true? Also read that I could change dataType to 'jsonp' - but this doesn't even work in dreamweaver.
Any ideas what could be wrong? Or should I use a whole other thing than AJAX for this problem?
This is a mobile app in PhoneGap, so I am also using jquery.
Upvotes: 1
Views: 289
Reputation: 116
Here is what i think is possible, as you use PhoneGap. You can test it with IE (not other browsers)
var xhr = new XMLHttpRequest();
xhr.open("GET", "url", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var text = xhr.responseText;
alert(text);
}
}
xhr.send();
Hope this works, it does for me at least. after this you can parse to xml.... and continue your work as you intended.
Upvotes: 1
Reputation: 198324
You read correctly, this is cross-scripting vulnerability prevention at work. You can use JSONP to get around it, but JSONP is not JSON - syntax is slightly different, so the server you're contacting has to send you the data in JSONP format. If you're expecting JSONP and receiving JSON, no wonder it's not working anywhere. If you're expecting JSONP and receiving XML, that's like apples and Toyotas.
Upvotes: 3