0

I am making the following ajax call from my HTTPS page,

 $.ajax({
    url: "http://www.w3schools.com",
    dataType: 'jsonp',
    crossDomain: true,
    complete: function (e, xhr, settings) {
        switch (e.status) {
            case 200:
                //everything fine
                break;
            default: 
                //Somethings wrong, show error msg                                   
                break;
        }
    }
});

But since I am making an HTTP call from an HTTPS page the call gets blocked. I can see a "insecure content warning" in console(CHROME),

but the error is not catched either in complete,error, or success. They simply don't fire at all.

I need to catch the error. Any way I can do this ?

wickjon
  • 900
  • 5
  • 14
  • 40

1 Answers1

1
 error
    Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

A function to be called if the request fails. The function receives three arguments: The 

jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that 

occurred and an optional exception object, if one occurred. Possible values for the second 

argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP 

error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not 

Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array 

of functions. Each function will be called in turn. Note: This handler is not called for 

cross-domain script and cross-domain JSONP requests. This is an [Ajax Event][1].

Try this,

find the status of request using jqXHR.status

Demo

$.ajax({
    url: "http://www.w3schools.com",
    dataType: 'jsonp',
    crossDomain: true,
    complete: function (e, xhr, settings) {
        switch (e.status) {
            case 200:
                //everything fine
                break;
            default: 
                //Somethings wrong, show error msg                                   
                break;
        }
    },
    error: function(jqXHR, textStatus, errorThrown){

    alert(errorThrown)
    },
});
dhana
  • 6,487
  • 4
  • 40
  • 63
  • Yes, this shows the alert in fiddle since it is an HTTP site. But trying this in a HTTPS site will not trigger the error :( – wickjon Feb 06 '14 at 06:40
  • @Targarian I updated my answer, If it is useful please accept my answer. Because next user follow this answer. Thanks – dhana Feb 06 '14 at 06:42
  • Yes, but still is there no way we can find such insecure content requests ? – wickjon Feb 06 '14 at 06:45
  • Yes you can find using `textStatus`. http://stackoverflow.com/questions/19262520/ajax-call-is-not-working-from-http-to-https – dhana Feb 06 '14 at 06:47
  • Yes, but I need error event to be fired in order to find the textStatus. Thats the issue. – wickjon Feb 06 '14 at 06:53