将php中的cURL POST转换为vanilla javascript

时间:2017-08-10 16:22:13

标签: javascript php curl post xmlhttprequest

我需要使用以下php代码并将其转换为javascript。我无法访问jQuery,所以它需要在vanilla javascript中。有人可以帮忙吗?

$username = "super";
$password = "duper";

$auth = base64_encode($username . ":" . $password);

$curl = curl_init();

$headers = array(
    'Authorization: Basic ' . $auth,
    'Content-Type: application/x-www-form-urlencoded',
    'Accept: application/json'
);

$fields = http_build_query(array(
    'action' => 'SKIP',
    'textword' => '12345',
    'number' => '1234567890'
));

$url = 'https://api.someplace.com/what/who';

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);

$resp = curl_exec($curl);

提前感谢任何可以提供帮助的人。

0 个答案:

没有答案