鍍金池/ 問答/HTML/ 為何ajax認(rèn)證不通過

為何ajax認(rèn)證不通過

服務(wù)器上已經(jīng)設(shè)置了cors
Header set Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, content-type"
Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"

basic auth 也設(shè)置好了
AllowOverride AuthConfig

服務(wù)器端進(jìn)一步的設(shè)置
cd /var/www/html && vim .htaccess
AuthName "login"
AuthType Basic
AuthUserFile /var/www/html/passwd
require user username
Create password for username.

htpasswd -c /var/www/html/passwd username
systemctl restart httpd

輸入111.111.111.111 跳出認(rèn)證窗口,輸入賬號,密碼,通過了,現(xiàn)在可以動手測試jquery代碼了。

function Ajax( ) {
    var username="xxxx";
    var password="xxxx";
    var url = 'http://111.111.111.111/remote.html';
    $.ajax(url, {
        type:"GET",
        dataType: 'html',
        crossDomain: true,
        headers: {
            "Authorization": "Basic " + btoa(username + ":" + password)
             },
        success:function(response){
            mytext = $("remote");
            mytext.append(response);
        },
        error: function (e) {
            alert("error");
        }
    });
};

上面的代碼無法通過驗證,請問,為何?

回答
編輯回答
她愚我
Header add Access-Control-Allow-Headers "origin, content-type, Authorization"
2017年12月20日 04:53