我将一些数据从ajax发送到一个php脚本,并将其传递给api。我注意到来自我的javascript的数据是console.log并且需要整数才能使用int但是当我改变我的php以将数据返回到我的脚本以便我可以确保它的结构正确时,我所有的ints以字符串形式返回。
示例:
的javascript
[{"UnitID":563614,"InsuranceID":null}]
PHP
[{"UnitID":"563614","InsuranceID":"NaN"}]
我很困惑为什么会这样。 javascript是如何应用的,但是php接口想要在接收数据时将所有的int转换为字符串。
以下是我的脚本的相关部分
的javascript
function wssReserve(form) {
var sendinsid = parseInt(form.InsuranceID.value);
data = {
"Units": [{
"InsuranceID": sendinsid
}]
};
var settings2 = {
"async": true,
"crossDomain": true,
"url": "php.script",
"data": { json: { data } },
"method": "POST"
};
var a2 = $.ajax(settings2);
$.when(a2).done(function(d2) {
console.log(JSON.stringify(d2));
});
}
PHP
function wssLocationReserve($data) {
header('Content-Type: application/json');
$url = 'external.api';
$auth = 'Authorization: Basic auth-token';
//$post_data = json_encode($_POST['data'});
$ch = curl_init($url);
$options = array(
CURLOPT_HTTPHEADER => array(
$auth,
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_decode($data));
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
return $data;
}
$json = $_POST['json'];
if( strcasecmp($_GET['method'],'hello') == 0){
$response['code'] = 1;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = wssLocationReserve($json);
}
deliver_response($_GET['format'], $response);
如果我返回$ result而不是$ data,我从api
获取 {\"Success\":false,\"ErrorMessage\":\"An unhandled exception has occurred and been logged.\"}