1

I deployed my website in IIS with https protocol. It works fine with http but ajax jquery request is failed with https. (I simply call a http web api which returns json data)

NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://....."

Did you get the same kind of errors when deploying with https?

Remember that this code below works well when deploy with HTTP, but switch to HTTPS it went to error section

var dataGetter = {
    authenticate: function (username, password) {
        var getTokenUrl = "http://xxx";
        var getTokenParams = { "username": username, "password": password }
        var result = false;        
        $.ajax({
            type: "GET",
            url: getTokenUrl,
            data: getTokenParams,
            contentType: "text/plain",
            dataType: 'json',
            crossDomain: true,
            cache: false,
            success: function (result) {
                  // do something here
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
                alert(XMLHttpRequest.responseText);
            },
            async: false
        });

        return result;
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bac Clunky
  • 353
  • 3
  • 6
  • 18
  • 1
    Cross domain request? – axel.michel Dec 09 '14 at 08:31
  • @axel.michel I already configured it and set to true Remember that it still works good when I deployed it with http protocol – Bac Clunky Dec 09 '14 at 08:43
  • @Jai I already enabled Remember that it still works good when I deployed it with http protocol – Bac Clunky Dec 09 '14 at 08:44
  • Can you provide some code, so we can have a look? – axel.michel Dec 09 '14 at 08:49
  • @axel.michel I edited the post with a code update – Bac Clunky Dec 09 '14 at 08:55
  • crossDomain? Do you use the jQuery Ajax Cross Origin Plugin? Otherwise it has to be dataType: 'jsonp' – axel.michel Dec 09 '14 at 09:00
  • @axel.michel thanks axel. I have some questions: 1. Do I have to use jQuery Ajax Cross Origin Plugin to get it work? 2. why do I have to set the dataType: "jsonp". Because it still works well when deployed with http – Bac Clunky Dec 09 '14 at 09:25
  • if you use 'crossDomain' you'll need the plugin, if you use dataType 'jsonp' you don't. Both are variants to enable cross-domain requests. And yes, HTTP and HTTPS is something different. It violates JavaScript's same-origin policy, because it doesn't see the HTTPS URL as being from the same source as the HTTP URL. – axel.michel Dec 09 '14 at 09:29
  • @axel.michel after add a Ajax Cross Origin Plugin, replace crossDomain to crossOrigin:true and dataType to jsonp. I get a message "jQuery111008997535526286811_1418117606943 was not called" – Bac Clunky Dec 09 '14 at 09:34
  • @BacClunky do not use both, and please have a look at jsonp - and what actually happens in case you'll use it, and how the plugin works. – axel.michel Dec 09 '14 at 10:01
  • @axel.michel I use an Ajax Cross Origin Plugin (dataType:json, not jsonp), no error was shown but It did not go to the success place and error place as well. Nothing happended. – Bac Clunky Dec 09 '14 at 10:11
  • 1
    @BacClunky I still recommend to read a bit: http://www.ajax-cross-origin.com/how.html – axel.michel Dec 09 '14 at 10:22
  • 1
    Possible duplicate of [HTTP Ajax Request via HTTPS Page](http://stackoverflow.com/questions/4032104/http-ajax-request-via-https-page) – albciff Nov 23 '16 at 12:12

1 Answers1

2

You cannot. Its the basic implicit security in browsers which doesn't allow you to call a insecure link (http) from https page.

For example:, Google Chrome will give you following error:

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