weixin_33696822 2017-03-14 10:05 采纳率: 0%
浏览 65

带有身份验证的Ajax

I need to create the same request in AJAX.

Can somebody advice me how to include authentication?

i tried user/pass as GET Parameters and change URL to

https://user:pwd@www.example.com/token

i use vue-resource but can also use jQuery.ajax or axios

$username = 'user';
$password = 'pwd';

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'https://www.example.com/token');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
  • 写回答

1条回答 默认 最新

  • weixin_33682790 2017-04-21 10:55
    关注

    try to use jQuery's beforeSend callback to add HTTP header with your authentication load:

    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
    },
    
    评论

报告相同问题?