dongtangu8403 2016-01-26 00:50
浏览 92
已采纳

从jQuery $ .ajax到PHP:访问传递的JSON值的麻烦

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!

  • 写回答

1条回答 默认 最新

  • dongsi7759 2016-01-26 08:58
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条