2

I'm trying to make a jquery Ajax call from an HTTPS website to an HTTP endpoint.

I'm getting a "Mixed Content" Error:

Mixed Content: The page at 'https://...' was loaded over HTTPS, 
but requested an insecure XMLHttpRequest endpoint 'http://...'. 
This request has been blocked; the content must be served over HTTPS.

The problem is that my callback on Ajax event 'complete' does not fire although according to the jquery page (https://api.jquery.com/Ajax_Events/):

complete (Local Event)
This event is called regardless of if the request was successful, or not. 
You will always receive a complete callback, even for synchronous requests.

Here is my code:

$.ajax({
    type: "POST",
    url: url,
    data: data,
    complete: function (results) {
        // Stuff in here is not executed on "Mixed Content" error
    }
});

Is there any other way to catch the error? Ajax doesn't seem capable.

EDIT I am aware of this question: HTTP Ajax Request via HTTPS Page

My question is not "why is the error occuring", I'm expecting it to happen. My question is how can I catch that type of error since jQuery's 'complete' event fails.

Community
  • 1
  • 1
filkaris
  • 41
  • 6
  • Have you tried `error()`? – Nick Zuber Mar 18 '16 at 00:14
  • Yes but it doesn't work. I think complete() is triggered on both error() and success() so it doesn't look like ajax can catch this on its own :/ – filkaris Mar 18 '16 at 00:19
  • Interesting... Could you recreate this issue in a fiddle by chance? – Nick Zuber Mar 18 '16 at 01:51
  • Yes it's quite simple since jsfiddle is an HTTPS page. https://jsfiddle.net/ueLq9toj/ After running the fiddle, check your browser console to see the error, but not the message. – filkaris Mar 18 '16 at 13:12
  • Have you tried checking the url for `http` vs `https` before you attempt to make the ajax request, and basically handling it before it becomes an error? Or are you just trying to find a less "hacky" way? – Nick Zuber Mar 18 '16 at 17:57
  • Yes that would be a viable solution. However I would like to know if there is a way for javascript/jquery to catch browser-related errors like this without having to handle them case-by-case. Today it's the "Mixed Content", tomorrow it might be something else... – filkaris Mar 18 '16 at 18:14
  • Looking at the error stack trace, it looks like when you attempt to make this kind of illegal call, JQuery itself handles this by throwing an error. I tried wrapping the AJAX call in a try/catch block and that didn't work – Nick Zuber Mar 18 '16 at 18:17

0 Answers0