Beginner
Beginner

Reputation: 29533

jquery parse json issue

I have got one app running on two servers, one works and one doesnt cant understand why.

ajax query..

 $.ajax({
        type: "POST",
        url: "WebService.asmx/GetFinish1",
        data: '{' +
                        'Item:"' + item + '"' +
                   '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var data = jQuery.parseJSON(msg);
            $("#item").val(JSON.stringify(msg));

at line $("#item").val(JSON.stringify(msg)); from one i get...

{"d":"{\"0\":{\"i\":\"MODIFIED C/W TOOL MC WALL\",\"D\":\"Notes\",\"V\":\"MODIFIED C/W TOOL MC WALL\"},\"1\":{\"i\":\"EA\",\"D\":\"Unit\",\"V\":\"EA\"},\"2\":{\"i\":\"EA\",\"D\":\"Unit\",\"De\":\"Unit\",\"V\":\"EA\"}}"}

the second i get

"{\"0\":{\"i\":\"1x 8351-3  &  2 x 8352-3\",\"D\":\"Notes\",\"V\":\"1x 8351-3  &  2 x 8352-3\"},\"1\":{\"i\":\"PC3\",\"D\":\"Unit\",\"V\":\"PC3\"},\"2\":{\"i\":\"PC3\",\"D\":\"Unit\",\"De\":\"Unit\",\"V\":\"PC3\"}}"

the second one works, the first doesnt, but why the difference? same code! where does the extra {} and d: come from?

Upvotes: 0

Views: 338

Answers (2)

Beginner
Beginner

Reputation: 29533

I don't know why, but I had to use the following var data = jQuery.parseJSON(msg.d);

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

You don't need

var data = jQuery.parseJSON(msg);
$("#item").val(JSON.stringify(msg));

Because, msg is already JSON formatted due to dataType: 'json' used in ajax property.

Upvotes: 1

Related Questions