授权基本身份验证在php中使用javascript进行休息调用失败

时间:2012-11-17 13:17:57

标签: php javascript rest basic-authentication

来自javascript的API调用失败,并且从PHP卷曲它们没问题。

用于检查身份验证的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 call

$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);

0 个答案:

没有答案