如何使用PHP解码Json数组(Ajax json帖子到php无法正常工作)

时间:2015-08-07 21:39:22

标签: php jquery ajax json

我是php新手我有基于jquery / javascript的应用程序,它涉及查找附近的地方并按排序顺序给出结果。我正在尝试将排序功能从html页面分离到服务器端php。

  1. 它涉及向" http://thewebby.net16.net/SortJson.php 发送和ajax请求" ,json数组作为参数

  2. 解析json请求并对其进行排序

  3. 基于这些SO问答

    Sending JSON to PHP using ajax

    Parsing JSON file with PHP

    Jquery的

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/SortJson.php",
            data: {myData:datas},
            success: function(data){
                alert('Items added');
            },
            error: function(e){
                console.log(e.message);
            }
    });
    

    PHP

    <?php
    
    if(isset($_POST['myData'])){
     $obj = $_POST['myData'];
     //some php operation
     $jsonIterator = new RecursiveIteratorIterator(
        new RecursiveArrayIterator(json_decode($obj, TRUE)),
        RecursiveIteratorIterator::SELF_FIRST);
    
    foreach ($jsonIterator as $key => $val) {
        if(is_array($val)) {
            echo "$key:\n";
        } else {
            echo "$key => $val\n";
        }
    }
    }else{
     echo "File Working";
    }
    ?>
    

    该页面位于http://thewebby.net16.net 在搜索对象按钮期间检查网络活动时,应该进行ajax调用。但是当我在chrome开发人员工具中检查网络活动时它没有发生

    JSON DATA

    [{"place":"IDBI Bank ATM","Distance":0.615,"Lat":8.531954,"Lng":76.92878100000007},{"place":"South Indian Bank ATM","Distance":0.662,"Lat":8.52868,"Lng":76.92865400000005},{"place":"IndusInd Bank","Distance":0.919,"Lat":8.525279,"Lng":76.928992},{"place":"SBT ATM","Distance":1.118,"Lat":8.522826,"Lng":76.92875000000004},{"place":"SBT ATM","Distance":0.7,"Lat":8.525756,"Lng":76.92017099999998},{"place":"Axis Bank ATM","Distance":0.771,"Lat":8.538183,"Lng":76.92333299999996},{"place":"Canara Bank ATM","Distance":0.662,"Lat":8.530059,"Lng":76.929123},{"place":"Indian Overseas Bank ATM","Distance":0.651,"Lat":8.528429,"Lng":76.92841399999998},{"place":"State Bank ATM","Distance":0.924,"Lat":8.539249,"Lng":76.92552699999999},{"place":"Corporation Bank ATM","Distance":1.019,"Lat":8.524513,"Lng":76.92949099999998},{"place":"Catholic Syrian Bank","Distance":0.715,"Lat":8.529943,"Lng":76.92959100000007},{"place":"United Bank of India ATM","Distance":1.015,"Lat":8.529396,"Lng":76.93226000000004},{"place":"Canara Bank","Distance":0.656,"Lat":8.530155,"Lng":76.92908699999998},{"place":"State Bank Of India e-Corner","Distance":0.824,"Lat":8.538184,"Lng":76.92589099999998},{"place":"punjab national bank ATM","Distance":0.625,"Lat":8.53369,"Lng":76.92835500000001},{"place":"STATE BANK OF TRAVANCORE ATM","Distance":0.964,"Lat":8.539663,"Lng":76.92535299999997},{"place":"Bank of Baroda","Distance":0.839,"Lat":8.529734,"Lng":76.930702}]
    

    编辑: -

    $.post( "SortJson.php",{myData:datas} , function( data ) {
     alert('Items added'+data);
    
    });
    

    当使用$ .post而不是$ .ajax方法工作时,请求正在发送。但是在服务器端,php文件总是进入其他情况

    if(isset($_POST['myData'])){..}
    else{
     echo "File Working";
    }
    

    如何解析php中的以下数据,我收到Invalid参数错误,传递的变量既不是对象也不是数组。更新网站中的代码$.Post而不是$.ajax

    Json Data is sent as this

    编辑2: -

    var_dump的结果

    array (
      0 => 
      array (
        'place' => 'GreenPepper',
        'Distance' => '0.487',
        'Lat' => '8.52699',
        'Lng' => '76.92419100000006',
      ),
      1 => 
      array (
        'place' => 'BAKE \'N\' COOL',
        'Distance' => '0.513',
        'Lat' => '8.527908',
        'Lng' => '76.92643599999997',
      ),
    )
    

    对于此json输入

    [{"place":"GreenPepper","Distance":0.487,"Lat":8.52699,"Lng":76.92419100000006},{"place":"BAKE 'N' COOL","Distance":0.513,"Lat":8.527908,"Lng":76.92643599999997}]
    

2 个答案:

答案 0 :(得分:0)

数据不是json编码。它是由@Quentin指出的形式编码所以不使用Json Iterator我只使用它而没有编码到json作为正常的post参数

for ($row = 0; $row < sizeof($obj); $row++) {


echo $obj[$row]['place'];

}

答案 1 :(得分:-1)

在文件的开头使用此代码。

<?php
    $postdata = json_decode(file_get_contents("php://input"));
        foreach($postdata as $key => $value){
        $credentials[$key] = $value;
        };
     print_r($credentials) //should return you the php accessible array instead of object.//only for testing purpose


     /*
        You logic to check the database or anyother operation
     */