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));
    },
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用