jQuery.ajax troubles, can someone help?

Forums » General Opera topics » User JavaScript

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

13. March 2012, 12:03:57

frankdd89

Posts: 96

jQuery.ajax troubles, can someone help?

I'm trying to fetch the html GET response from a webpage with this code
window.addEventListener('load', function () {start()} ,false);
function start(){
         with(window){  			
                  /*Loading jQuery here */
                  jQuery.ajax({  			
                           url: 'http://api.jquery.com/jQuery.ajax/',  			
                           type: 'GET',  			
                           success: function(res) {  				
                           alert("Done!");  },  			
                           error: function (XMLHttpRequest, textStatus, errorThrown){	
                                    alert(XMLHttpRequest.status); 
                                    alert(XMLHttpRequest.responseText); }  			
                  });  
         };
}


but I had no luck, it gives me '0' at XMLHttpRequest.status, and 'undefined' to XMLHttpRequest.responseText.
Can someone help me get out of this?

14. March 2012, 08:52:46

Frenzie

Posts: 14442

It could be that your use of with is causing trouble. It's not allowed in strict mode.
Intelligent alien life does exist, otherwise they would've contacted us. — CalendarExtend Opera

16. March 2012, 01:34:22

spadija

Posts: 1636

It could also be that, unless you're calling this from jquery.com, that's a cross origin request. There's an article on dev.opera about this, but I'm not entirely sure how jQuery handles cross origin requests. From what I understand, if jQuery's server isn't set up to allow cross origin resource sharing, you won't be able to download jQuery via XHR from a user script. (You could use an extension though, as the background script can make cross origin requests.)

16. March 2012, 09:51:43

Frenzie

Posts: 14442

Originally posted by spadija:

It could also be that, unless you're calling this from jquery.com, that's a cross origin request


Shouldn't that result in an error in the error console?

Anyway, to expand on what I was saying, it can be ambiguous to what the with should apply precisely.
Intelligent alien life does exist, otherwise they would've contacted us. — CalendarExtend Opera

16. March 2012, 16:03:28

spadija

Posts: 1636

Originally posted by Frenzie:

Shouldn't that result in an error in the error console?


Maybe if you were using plain XHR, but jQuery could be swallowing the error.

16. March 2012, 16:16:52

Frenzie

Posts: 14442

Originally posted by spadija:

Maybe if you were using plain XHR, but jQuery could be swallowing the error.


Ah, good point.
Intelligent alien life does exist, otherwise they would've contacted us. — CalendarExtend Opera

Forums » General Opera topics » User JavaScript