1

Having browsed SO for hours now and tried various proposed solutions to similiar problems so as having read the official jQuery Docs, I still can't get the following code to work and would appreciate any help and hints to what I'm doing wrong.

I basically try to pass a custom JSON-object using jQuery $.ajax post to a PHP AJAX-Handler, but my PHP-Script always tells me that the data is invalid and won't execute.

jQuery JavaScript Code:

function $('#linkButton').click(function(){
    var latlng = '47.39220630060216,9.366854022435746';
    var locationData = JSON.stringify({
            from_mobile: '1',
            location: latlng
        });
    $.ajax({
        type: 'post',
        url: 'ajax_handler.php',
        data: locationData,
        success: function (data) {
            console.log(data); // returns 'Invalid location: ' from PHP
        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus + ' ' + errorThrown);
        }
    });
});

Interestingly, when I log the "locationData" json-object, it looks just fine:

{"from_mobile":"1","location":"47.39220630060216,9.366854022435746"}

PHP AJAX-Handler Code 'ajax_handler.php':

$mob = json_decode($_POST['from_mobile']);
$loc = json_decode($_POST['location']);

if(!empty($loc))
{
    echo myClass::theClassMethod($mob, $loc);
} else {
    exit('Invalid location: '.$loc);
}

Can anyone here spot the issue why I can't read the JSON-Values in my PHP-Script? Any help is highly appreciated!

Community
  • 1
  • 1
Oliver
  • 416
  • 5
  • 14
  • 1
    Change: `data: locationData` to: `data {stuff : locationData}` (without quotes around `stuff`). Access it through $_POST['stuff']. Working now? – jhmckimm Jan 26 '16 at 01:03
  • Amazing, you pointed me into the right direction - got it to work. Thank you! – Oliver Jan 26 '16 at 08:43

1 Answers1

0

Thanks to @DarthGualin 's comment and this SO answer I got it to work successfully changing the passing (JS) & parsing (PHP) Codes as follows:

jQuery JavaScript Code:

Change:

data: { jsonData: locationData },

Full Code:

function $('#linkButton').click(function(){
    var latlng = '47.39220630060216,9.366854022435746';
    var jsonData = JSON.stringify({
            from_mobile: '1',
            location: latlng
        });
    $.ajax({
        type: 'post',
        url: 'ajax_handler.php',
        data: { jsonData: locationData },
        success: function (data) {
            console.log(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus + ' ' + errorThrown);
        }
    });
});

PHP AJAX-Handler Code 'ajax_handler.php':

Change:

$data = json_decode($_POST['jsonData']); // Access Data Object
$mob = $data->{'from_mobile'}; // Access Value from json_data

Full Code:

$data = json_decode($_POST['jsonData']);
$mob = $data->{'from_mobile'};
$loc = str_replace(' ', '', $data->{'location'});

if(!empty($loc))
{
    echo myClass::theClassMethod($mob, $loc);
} else {
    exit('Invalid location: '.$loc);
}
Community
  • 1
  • 1
Oliver
  • 416
  • 5
  • 14