weixin_33695082 2017-06-22 10:32 采纳率: 0%
浏览 189

带密码的AJAX请求

I have the following PHP/CURL. I need to send it via JS/AJAX (Axios)

How to adapt to JS?

$username = 'test';
$password = 'test';

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'https://example.com/pc_api/index.php/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);
  • 写回答

1条回答 默认 最新

  • weixin_33724059 2017-06-22 11:36
    关注

    To use with axios, Use auth for basic authentication :

    axios({
        method: 'get',
        url: 'https://example.com/pc_api/index.php/token',
        responseType: 'json', // default is json
        auth: {
            username: 'test',
            password: 'test'
        }
    }).then(function(response) {
        console.log(response);
    }).catch(function(error) {
        console.log(error);
    });
    

    Check request config params

    评论

报告相同问题?