1

I've dealing with this issue for the past month, I've read almost all the posts here at stackoverflow, but no way... my best approach was:

$.ajax({ url: "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js", dataType: "script", cache: false, xhr: function () { var xhr = new window.XMLHttpRequest(); //Not working... xhr.onerror = function(e) { alert("Error Status: " + e.target.status); }; //Frustration ends here... return xhr; } }) .done(function(data) { console.log("Ajax success"); init(); callback(); }); 

The "connect1" makes the request to fail, but I cant get a way to catch the error because of the famous issue with crossdomain requests. My best bet was to implement my own xhr object, but the onerror event is not being triggered.

I wrote this post because any new or fresh idea will help me a lot.

UPDATE: This is the url that you can use to replicate the request, that is intended to fail: https://connect1.facebook.net/es_LA/sdk.js?_=1445285186108

Thanks so much in advance.

Guillermo

15
  • Why not just add the script to head? Commented Oct 19, 2015 at 19:43
  • I may be way off, but in the past when working with FB and youtube API's in jquery, I have had to add callback=? to the url to get any sort of response. Commented Oct 19, 2015 at 19:44
  • Also, to double check, what is a sample URL you expect as connect1.facebook.net/pt_BR/sdk.js looks invalid Commented Oct 19, 2015 at 19:46
  • 2
    @Guillermo it's not entirely that it is cross domain, it is a script request is more critical issue. Cross domain CORS enabled requests will return all normal errors Commented Oct 19, 2015 at 19:54
  • 1
    Possible duplicate of JSONP request error handling Commented Oct 19, 2015 at 21:34

2 Answers 2

0

To check for response codes, which is different than script errors, add a statusCode callback to your ajax.

$.ajax({ url: "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js", dataType: "script", cache: false, statusCode: { 404: function() { alert( "page not found" ); } }, xhr: function () { var xhr = new window.XMLHttpRequest(); //Not working... xhr.onerror = function(e) { alert("Error Status: " + e.target.status); }; //Frustration ends here... return xhr; } }) .done(function(data) { console.log("Ajax success"); init(); callback(); }); 

Also, since the URL appears to be JS, and you arnt against using JQuery, why not make use of jQuery.getScript()?

$.getScript( "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js"); 
Sign up to request clarification or add additional context in comments.

4 Comments

thanks so much, but not working, I took a look to the console to check the statusCode, but can´t get one, it just only says (failed). You can try to replicate the failed request with the following url: connect1.facebook.net/es_LA/sdk.js?_=1445285186108
Hmm after further tests I see what you mean, perhaps this can help stackoverflow.com/questions/19035557/…
I still recommend the getScript method to clean up code a little easier regardless.
We we're using it, but due that is a shorthand, I moved to the regular ajax method to have more options to work with. Thanks for all your support!
-1

Finally, thanks to @Wobbles who helped me a lot, we found the answer here:

https://stackoverflow.com/a/19101142/943082

The solution consists on adding a timeout to the ajax request.

After that timeout has expired, then the onerror event will be triggered, and from there, you can respond to the error with your code.

 $.ajax({ url: "https://connect.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js", dataType: "script", cache: false, timeout: 5000, // Wait 5 secs to get the sucess error: function(jqXHR, textStatus, errorThrown) { if(textStatus === "timeout") { //We waited 5 secs and the request hasnt succeed. //Code to manage the error. } } }) 

Regards,

Guillermo

1 Comment

Flagged as duplicate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.