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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀