0

来自 javascript 的 API 调用失败,而来自 PHP curl 他们很好。

用于检查身份验证的 PHP 脚本


 if (!isset($_SERVER['PHP_AUTH_USER'])) {
        header('WWW-Authenticate: Basic realm="Test auth"');
        header('HTTP/1.0 401 Unauthorized');
        echo '{"status":"401","message":"Unauthorized"}';
        exit;
    } else {
        $user= $_SERVER['PHP_AUTH_USER'];
        $pass =  $_SERVER['PHP_AUTH_PW'];
    }

Javascript // 失败

var xhr = new XMLHttpRequest();
var apiid = 'admin';
var apipassword = '123';
var requesturl = 'http://localhost/man/api/man/GetLanguages';
xhr.open('POST', requesturl, false, apiid, apipassword);
xhr.send('{}');
if(xhr.status == 200){
     alert(xhr.responseText);
} 

PHP curl 调用

$ch = curl_init($url);                                                                
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($data),'Authorization: Basic YWRtaW46MTIz'));     
echo $result = curl_exec($ch);
4

0 回答 0