簡體   English   中英

jQuery解析JSON問題

[英]jquery parse json issue

我有一個在兩台服務器上運行的應用程序,一台可以運行,一台無法理解原因。

ajax查詢..

 $.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));

$("#item").val(JSON.stringify(msg)); 從一個我得到...

{"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\"}}"}

我第二次

"{\"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\"}}"

第二個有效,第一個無效,但為什么有所不同? 相同的代碼! 額外的{}和d:從何而來?

你不需要

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

因為,由於在ajax屬性中使用了dataType: 'json' ,因此msg已經采用JSON格式。

我不知道為什么,但是我必須使用以下var data = jQuery.parseJSON(msg.d);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM