0

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

3
  • I just tried JSON.parse on it and it worked okay Commented Apr 5, 2013 at 0:31
  • 2
    No need to parse if you already set datatype=json.. Because it will already parse the JSON string as a javascript object Commented Apr 5, 2013 at 0:33
  • tha parsing error is because of the ,(comma) after the last object of the string ie after the last '{ "title": " Nac", "no1": "1212","no2": "12126"}' you have to concatenate data with ] and not ,] Commented Oct 27, 2014 at 19:52

1 Answer 1

1

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',
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.