0

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.\"}
31
  • perhaps deliver_response messes 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 checked deliver_response to see it's not messing with the data? Didn't you ask this question about an hour ago? Commented Jul 5, 2017 at 5:26
  • @adeneo I don't see what your talking about. Commented Jul 5, 2017 at 5:28
  • In php, where is $json defined? Commented Jul 5, 2017 at 5:29
  • @jan.vogt I added it. I accidentally left it out when I trimmed down the example. Commented Jul 5, 2017 at 5:31
  • 1
    check the browser developer tools network tab to see exactly what the browser is sending in the POST request, you will see that the browser is a) NOT sending JSON, and b) sending NaN instead of null Commented Jul 5, 2017 at 6:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.