I am sending some data from ajax to a php script that passes it to an api. I noticed that the data being console.log'd from my javascript has ints where ints are required but when I altered my php to return the data back to my script so that I could make sure it is structured correctly, all of my ints come back as strings.
example:
javascript
[{"UnitID":563614,"InsuranceID":null}]
php
[{"UnitID":"563614","InsuranceID":"NaN"}]
I am confused as to why this is happening. How the javascript has it is how it should be, but php seams to want to convert all of the ints to strings when it receives the data.
Here is the relevant part of my scripts
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);
if I return the $result instead of $data I get this from the api
{\"Success\":false,\"ErrorMessage\":\"An unhandled exception has occurred and been logged.\"}
deliver_responsemesses with the data? perhaps the external API is sending you exactly what you are sending from PHP? have you debugged the PHP to see what it is receiving? have you checkeddeliver_responseto see it's not messing with the data? Didn't you ask this question about an hour ago?