跨域ajax调用问题

时间:2015-09-18 09:50:06

标签: ajax authentication cross-domain restful-authentication

我正在编写在https://页面上运行的代码。我想与我的页面中的http://页面进行通信。

我正在使用以下代码:

var USERNAME = "*****";
var PASSWORD = "****"

$.ajax({
    type: "POST",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
     headers: {
      "Authorization": encodeURI("BASIC " + btoa(USERNAME + ":" + PASSWORD))
    },
    url: "http://111.93.214.93/data",
    data: JSON.stringify(data)
})
.done(function(response) {
    alert("Shuffled image successfully")
})
.fail(function(jqXHR, textStatus, errorThrown) {
    console.log(jqXHR, textStatus, errorThrown);
    alert("Server Error");
})
.always(function(jqXHROrData, textStatus, jqXHROrErrorThrown) {});

这是正确的方法吗?

我收到“404 Not Found”错误。

我也试过这个。但它没有帮助

type: "POST",
                    contentType: 'application/json; charset=utf-8',
                    async:false,
                    crossDomain: true,
                    callback: '?',
                    dataType: 'jsonp',
                    headers: {
                      "Authorization": "Basic " + btoa(unescape(encodeURIComponent(USERNAME + ":" + PASSWORD)))
                    },

1 个答案:

答案 0 :(得分:0)

根据W3,由于“证书错误”http://www.w3.org/TR/access-control/#user-agent-security

,通过COR政策是不可能的

但你可以试试这个 从服务器添加Access-Control-Allow-Origin标头

访问控制 - 允许 - 来源:https://www.example.com
http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing