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!

2
  • 1
    Change: data: locationData to: data {stuff : locationData} (without quotes around stuff). Access it through $_POST['stuff']. Working now? Commented Jan 26, 2016 at 1:03
  • Amazing, you pointed me into the right direction - got it to work. Thank you! Commented Jan 26, 2016 at 8:43

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.