0

Here is my code,I want to request a cross domain url to use API based on basic authorization.

but it does not work. no Authorization info showed in request headers.

$(document).ready(function () {
    $.ajaxSetup({
        headers: {
            'Authorization': "Basic amlhemhc6SUxvdmVAkYXk="),
            'Content-Type': 'application/json;charset=utf-8'
        }
    });
    var request = $.ajax({
        url: "http:localhost:8080/query",
        type: "POST",
        data: '{"sql":"select count(*) from summary"}',
        dataType: "json"
    });
    request.done(function (msg) {
        alert(msg);
    });

    request.fail(function (jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    });
});
enter image description here
Aditya
  • 1,241
  • 5
  • 19
  • 29
jason zhong
  • 101
  • 1
  • 9

2 Answers2

0

Try adding the following callbacks to your $.ajax():

var request = $.ajax({
        url: "http:localhost:8080/query",
        type: "POST",
        data: '{"sql":"select count(*) from summary"}',
        dataType: "jsonp",
        processData: 'false',
        xhrFields: {
              withCredentials: 'true'
        },
        crossDomain: 'true'
    });
Aditya
  • 1,241
  • 5
  • 19
  • 29
  • to support cors domain request,I add Access-Control-Allow-Origin:* in the response.because of which .when I modify as you suggested ,throw error like this( A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:9000' is therefore not allowed access.) – jason zhong Jan 06 '15 at 08:31
  • have you tried entering the exact domain instead of using `*`? Since we are using credentials, you will get this error as it is a part of security. `Access-Control-Allow-Origin:your domain` – Aditya Jan 06 '15 at 08:38
  • yes ,but still response 401 status code.and no field Authorization in the Request Headers,it's supposed there. – jason zhong Jan 06 '15 at 08:45
0

your ajax should look something like this, if it still doesnt work then you can take a look one of my post about CORS here and see what issue you are facing

ajaxOpts = {
        url:"https://"+server_name+"/post_link/",
        crossDomain:true,
        xhrFields: {withCredentials:true},
        type:'POST',
        data: jQuery('#some_form').serialize(),//or your data
        dataType:'json',
        success: {
            //do something
        },
        error: {
            //do something
        },
    };
    jQuery.ajax(ajaxOpts);

hope it helps

Aameer
  • 1,366
  • 1
  • 11
  • 30