简体   繁体   English

如何使用PHP解码Json Array(将Ajax json发布到php无效)

[英]How To Use PHP to decode Json Array(Ajax json post to php not working)

I am new to php I have jquery/javascript based application which involves finding nearby places and give the result in a sorted order .I am trying to separate the sort function from html page to a server side php. 我是php的新手,我有一个基于jquery / javascript的应用程序,该应用程序包括查找附近的地方并按排序顺序给出结果。我试图将html页面的排序功能分离到服务器端php。

  1. It involves sending and ajax request to " 它涉及发送和ajax请求到 http://thewebby.net16.net/SortJson.php " with json array as parameter 以json数组作为参数

  2. Which parses the json request and sorts it 解析json请求并对其进行排序

Based On these SO Question and Answer 基于这些SO问答

Sending JSON to PHP using ajax 使用ajax将JSON发送到PHP

Parsing JSON file with PHP 用PHP解析JSON文件

Jquery 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

<?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";
}
?>

The page is hosted at http://thewebby.net16.net Upon checking the network activity during search for Objects Button the ajax call is supposed to be made.But its not happening when i checked network activity in chrome developer tool 该页面位于http://thewebby.net16.net 。在搜索“对象按钮”期间检查了网络活动后,应该进行了ajax调用。但是当我在chrome开发人员工具中检查了网络活动时却没有发生。

JSON DATA JSON数据

[{"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}]

EDIT:- 编辑:-

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

});

When $.post is used instead of $.ajax method its working,the request is being sent .But at the server side the php file is always entering else case of 当使用$ .post代替$ .ajax方法工作时,将发送请求。但是在服务器端,php文件始终进入其他情况

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

How to parse the following data in php ,I am getting Invalid argument error the passed variable is neither object or array.Am updating the code in the website with $.Post Instead of $.ajax 如何在php中解析以下数据,我收到无效参数错误,传递的变量既不是对象也不是array.Am使用$.Post而不是$.ajax更新网站中的代码

Json Data这样发送

EDIT 2:- 编辑2:-

Result Of var_dump 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',
  ),
)

For this json input 对于此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}]

The Data was not json Encoded .It was form encoded as pointed out by @Quentin So instead of using Json Iterator I just used it without encoding to json as normal post parameter 数据不是JSON编码的,它是按@Quentin指出的形式编码的,因此我没有使用Json Iterator而是使用了它而不将其编码为普通的post参数

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


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

}

use this code at the start of the file. 在文件开头使用此代码。

<?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
     */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM