Join the Stack Overflow Community
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In a webpage that uses javascript, I pass data to a hidden input field using

$("#waypt_sel").val(JSON.stringify(itins.arr_intin));

and then later access that data using

waypts_input = $.parseJSON($("#waypt_sel").val())

This works, except that sometimes it gives a

Uncaught SyntaxError: Unexpected end of input

. I tracked the error to this line with the json parsing. but I am baffled because this works sometimes but sometimes it doesn't for the same, identical string.

I checked the values that are passed to the html inputs and it works and doesn't work for the same values. Here's an example of the json string I am passing:

"[{\"location\":\"8.3353156, 80.3329846\",\"stopover\":true}, {\"location\":\"8.0326424, 80.7446666\",\"stopover\":true}, {\"location\":\"7.9577778, 80.667518\",\"stopover\":true}, {\"location\":\"7.953208, 81.006675\",\"stopover\":true}, {\"location\":\"7.885949, 80.651479\",\"stopover\":true},{\"location\":\"7.2905425, 80.5986581\",\"stopover\":true},{\"location\":\"7.300322, 80.386362\",\"stopover\":true}]"

Here's the structure of the code I use.

$(document).ready(function() {


    $.ajax({
        url: "aa.php",
        type: "POST",
        data: {
            id: selected_i
        },
        success: function(result) {

            itins = $.parseJSON(result);
            $("#waypt_sel").val(JSON.stringify(itins.arr_intin));
        }
    });


    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "dd.php",

        success: function(result) {
            locations = $.parseJSON(result);
            initializeMap();

        }
    });


    function initializeMap() {
    //other code
        calculateAndDisplayRoute();
//other code


        function calculateAndDisplayRoute() {
            //other code
            waypts_input = $.parseJSON($("#waypt_sel").val());
            waypts_json_input = $.parseJSON(waypts_input);
            //other code
            }

    }


});

And here is the detailed error message I get on firefox developer edition browser.

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

calculateAndDisplayRoute() map.js:366

initializeMap() map.js:290

.success() map.js:62

m.Callbacks/j() jquery-1.11.3.min.js:2

m.Callbacks/k.fireWith() jquery-1.11.3.min.js:2

x() jquery-1.11.3.min.js:5

.send/b() jquery-1.11.3.min.js:5

Thanks in advance.

share|improve this question
6  
Can you show an example of JSON.stringify(itins.arr_intin)? – Stefan Gehrig Nov 30 '15 at 9:54
1  
Show the code, show the json, make a working snippet/fiddle. See more: blog.stackoverflow.com/2014/09/… We aren't wizards with crystal balls. – Marcos Pérez Gude Nov 30 '15 at 9:55
1  
I know that this doesn't answer the question, but why don't you store the value in a simple javascript variable? – nagylzs Nov 30 '15 at 9:57
6  
"I am baffled because this works sometimes but sometimes it doesn't for the same, identical string." - I can't really believe that. – Bergi Nov 30 '15 at 10:00
2  
Why dont you just pass the variable in a value? Now that BAFFLES me! – burp_dude Nov 30 '15 at 10:32

"\" is unnecessary when deserialize json string in javascript .

what json tools you used?

you serialize in one tool , and deserialize with other , may get this scene .

share|improve this answer
2  
\ is just part of the string literal. – Ryan Nov 30 '15 at 10:41
    
This is because the this json array is part of a larger another json array. Need to escape double quotes when using json arrays inside json arrays, else it gave me errors – toing_toing Nov 30 '15 at 10:52
1  
@NisalUpendra: Well in that case it's really an antipattern. Don't put JSON in JSON strings inside JSON. – Bergi Nov 30 '15 at 11:34
up vote 0 down vote accepted

The issue was that I was using an asynchronous ajax request to retrieve json data. by the time the data had been retrieved and pasted to the html, the execution of the code that used the html data had happened, and thus gave an error. I used a callback function for the ajax query and this did the job.

function get_det(callback) {//Your asynchronous request.  
    $.ajax({
      url: "aa.php",
      type: "POST",
      success: function (result) {
        alert("1st call");
        callback();//invoke when get response 
      }
    });
  }

and in the code where this is called:

get_det(secondFunction);//calling with callback function
function secondFunction()//your callback function
{
 alert("2nd Call");
}

Alternatively you may also try async: false in the ajax query parameters. But this can cause browser freezing and is not recommended.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.