简体   繁体   中英

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.

  1. It involves sending and ajax request to " http://thewebby.net16.net/SortJson.php " with json array as parameter

  2. Which parses the json request and sorts it

Based On these SO Question and Answer

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

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

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}]

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

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

Json Data这样发送

EDIT 2:-

Result Of 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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