我正在尝试对外部 url 执行 ajax 请求。现在我在 php 中执行此操作
as
$data = array(
'TokenID' => $tokenid,
'APIKey' => $api_key,
'EcryptedData' => $encrypted_data,
'TokenScheme' => 4
);
//convert to JSON
$json = json_encode($data);
//curl config
$ch = curl_init("https://testingonetwo.com/rest/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json', //we are using json in this example, you could use xml as well
'Content-Length: '.strlen($json),
'Accept: application/json') //we are using json in this example, you could use xml as well
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$outputjson = curl_exec($ch);
我尝试使用 javascript 做同样的事情。我尝试使用 jquery,但没有成功
我尝试过的 jquery 代码是
$data = array(
'TokenID' => $tokenid,
'APIKey' => $api_key,
'EcryptedData' => $encrypted_data,
'TokenScheme' => 4);
$json = json_encode($data);
$.ajax({
type: "POST",
url: 'https://testingonetwo.com/rest/',
data: {data:$json},
success: success,
dataType: "json"
});
alert(result);
function success(result) {
alert('done');
}
我对客户端浏览器脚本都很陌生。请帮助我尝试上面的 JavaScript,我更喜欢它。期待一些帮助。