I have looked at a few post but cant seem to work out what I've done incorrect, i need to work with the JSON to inport into database but it saying my posted cities is not coming through?
Javascript
console.log(jsonData);
$.ajax({
url: "ajax.php",
type: "post",
data: jsonData,
datatype: 'json',
success: function(jsonData){
$('#result').html(jsonData);
},
error:function(){
alert('ajax failed');
}
});
}
Ajax.php
$obj = json_decode($_POST['cities']);
print_r($obj);
Error :
Notice: Undefined index: cities in ajax.php on line 3
Console Log for Json
{"currentCity":"Exeter","cities":[{"name":"Exeter","offers":[{"offer":"test","price":"11","notes":"100","city":"Exeter"}]}]}
Well firstly, datatype
needs to be dataType
. Note that this follows the lower camel case nomenclature.
Secondly, $_POST['cities'];
will not work because it is the key in a JSON object and not a key to a post identifier. What you need to do is pass it along like, data: {'json' : jsonData}
, then you can do:
$obj = json_decode($_POST['json']);
print_r($obj['cities']);
edit
So your ajax call will look like below:
$.ajax({
url: "ajax.php",
type: "post",
data: {
"json" : jsonData
},
dataType: 'json',
success: function(jsonData){
$('#result').html(jsonData);
},
error:function(){
alert('ajax failed');
}
});