I keep on getting a JSON parser error (firebug console say 'There are no child objects') for the following data:
(String) var data from each iteration
var data='[';
data+= '{ "title": " Nac", "no1": "1212","no2": "12126"},';
data+= '{ "title": " Nac", "no1": "1212","no2": "12126"},';
data+= '{ "title": " Nac", "no1": "1212","no2": "12126"},';
data+= ']';
and javascript parsing json
var json = JSON.parse(data)
and jQuery AJAX request
$.ajax({
type: "POST",
data: json,
url : 'ticket.php',
dataType: 'json',
async: false,
contentType : 'application/json; charset=utf-8',
error: function(jqXHR, exception)
{
if (jqXHR.status === 0)
{
$('.item').html("err");
} else if (jqXHR.status == 404)
{
$('.item').html('err!');
} else if (jqXHR.status == 500)
{
alert("err!");
} else if (exception === 'parsererror')
{
$('.item').html('err parsererror');
} else if (exception === 'timeout')
{
$('.item').html('err!');
} else if (exception === 'abort')
{
$('.item').html('err!');
} else
{
$('.item').html('err!');
}
},
success : function(data)
{
alert("okey");
}
});
and ticket.php is completely empty because I don't no how receive json data from ajax in php
Any help would be highly appreciated. Thnks
JSON.parse
gives you a JavaScript object, if you're sending json
in a post then send json not an object. Also instead of building a json string, build an object and then stringify it
var data= [{
"title": " Nac",
"no1": "1212",
"no2": "12126"
},
{
"title": "New",
"no1": "12",
"no2": "121"
},
{
"title": "San",
"no1": "1227",
"no2": "1"
}];
var json = JSON.stringify(data);
$.ajax({
type: "POST",
data: json,
url : 'ticket.php',
dataType: 'json',
async: false,
contentType : 'application/json; charset=utf-8',
...