I have looked at many posts on this forum and still having problems, can't get my head round it.
i am retreiving a php page through ajax which is posting a json object:
$.ajax({
url: myUrl,
data: sendData,
type: "POST",
error: function(xhr, statusText, errorThrown){
// Work out what the error was and display the appropriate message
},
success: function(data){
// data retrived ok
var myData = data;
// do something with data
}
});
my json is like this (generated with js):
{"borough": {"id": "3"}, "cat":{"id": "5", "id": "47", "id": "98"}}
using firebug i have hecked and its getting passed across
my problem is with the php, i cant seem to get the data json_decoded.
<?php
$catPost = $_POST['cat'];
echo($catPost);
$catData = json_decode($catPost, true);
var_dump($catData);
?>
the echo statement prints out [object Object]
but the var_dump prints out null
what am i doing wrong? how do i access the diffirent "id" values in the data????
any help greatly appreciated.
the test page can be seen at http://http://www.reelfilmlocations.co.uk/NEW Search/fullsearch_jq.php
Instructions for testing: (use thetext "Advanced search" under the select category dropdown to perform the ajax call also select a borough and somecategories topopulate the json object with some data
EDIT:
the json is created by the following script which reads values of selected tages from an array:
var sendData = {"borough":[], "cat":[]};
//alert('borough tagger add '+BoroughTagger.myIdArray[intIndex]);
sendData.borough.push({"id":BoroughTagger.myIdArray[intIndex]});
$.each(CatTagger.myIdArray, function(intIndex, objValue) {
alert('cat Tagger add '+CatTagger.myIdArray[intIndex]);
sendData.cat.push({"id":CatTagger.myIdArray[intIndex]});
});
which would create a json object like the following: (checked this in fiebug)
{"borough": {"id": "3"}, "cat":{"id": "5", "id": "47", "id": "98"}}
I am now using stringData = JSON.stringify(sendData)
to send the data via ajax but there dosent seem to be any name in the post collections, the string is there but if i reference it using $myData = $_POST;
which echos "ArrayArray" the json_decode errors with: Warning: json_decode() expects parameter 1 to be string, array given in D:\wamp\www\ReelFilm\NEW Search\getAdvSearch.php on line 7
so how do i get the ajax call to assign a name tothe sent sata so i can access it using $_POST['myData']
I've found a solution to your problem. Try this.
It may have been that as you are using jQuery, they may have posted it in a different way to cause PHP to recognize it as an object. I know you've done it in json_decode() but the echo() output only recognizes the variable as an object therefore you will need to force PHP to change it to array.
<?php
$catPost = (array) $_POST['cat']; /* PHP may post it as an object, this converts to array, which you'll need to find the data from */
echo($catPost);
$catData = json_decode($catPost, true);
var_dump($catData);
?>
If that doesn't work, base64_encode the jQuery ajax() data before pushing it to your server. You can find a base64_encode function at phpjs.org at phpjs.org and simply encode your string and then use PHP base64_decode() to decode the output and then json_encode().
Hope it helps!
If json_decode() gives you a null, use json_last_error() to try and identify the reason for the failure.
Try adding this option to your ajax-call:
traditional:false
looks like somehow the traditional-option of ajaxSetup is set to true(if it is , there will no recursive serialization be done on passed objects)