简体   繁体   English

解析要在AJAX成功调用中使用的JSON字符串

[英]Parse JSON string to use in AJAX success call

I have code that generates a JSON string. 我有生成JSON字符串的代码。 An example string that is generated by that code is as follows. 该代码生成的示例字符串如下。 I have validated it through the JSON lint tool and it validates perfectly. 我已经通过JSON lint工具对其进行了验证,并且可以完美验证。

{"ShipToName" : "(    ) CARPINTERIA         CA", "ShipToAddress1" : "3785 SANTA CLAUS LANE", "ShipToAddress2" : "", "ShipToCity" : "CARPINTERIA", "ShipToState" : "CA", "ShipToZip" : "93013", "ShipVia" : "UPS", "Terms" : "01", "SalesRep" : "KV1" }

I then have some JQuery that is going to parse that string. 然后,我有了一些将解析该字符串的JQuery。 Right now I am just trying to alert one of the parts of the string to be sure the code is working correctly. 现在,我只是想提醒字符串的一部分,以确保代码正确运行。 I have tried both of the following with no success: 我尝试了以下两种方法都没有成功:

Attempt #1: Alerts 'undefined' 尝试1:警报“未定义”

function hidShipTo_IndexChanged(sender, args) {
                var strComboID = $find("<%=rcbCustomer.ClientID%>");
                var strValue = strComboID.get_value();
                var strShipToVal = $("#hidShipTo").val();
                var strData = "{ strSoldToSelected: '" + strValue + "', strShipToSelected: '" + strShipToVal + "' }";
                alert("yes");
                $.ajax({
                    type: "POST",
                    url: "/webservices/ProductServer.asmx/PopulateShipToDetails",
                    data: strData,
                    contentType: "application/json; character=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.SalesRep);
                    },
                    failure: function (xhr, ajaxoptions, thrownError) {
                        alert("Error1:" + xhr.status);
                        alert("Error2:" + thrownError);
                    }
                });
            }

Attempt #2: throws an error inside of a JQuery library object 尝试2:在JQuery库对象内引发错误

function hidShipTo_IndexChanged(sender, args) {
                var strComboID = $find("<%=rcbCustomer.ClientID%>");
                var strValue = strComboID.get_value();
                var strShipToVal = $("#hidShipTo").val();
                var strData = "{ strSoldToSelected: '" + strValue + "', strShipToSelected: '" + strShipToVal + "' }";
                alert("yes");
                $.ajax({
                    type: "POST",
                    url: "/webservices/ProductServer.asmx/PopulateShipToDetails",
                    data: strData,
                    contentType: "application/json; character=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.SalesRep);
                    },
                    failure: function (xhr, ajaxoptions, thrownError) {
                        alert("Error1:" + xhr.status);
                        alert("Error2:" + thrownError);
                    }
                });
            }

Any ideas on how I can access each of the items in the JSON String? 关于如何访问JSON字符串中每个项目的任何想法?

Thanks! 谢谢!

since your code does not throw an undefined exception, then we can deduce 'msg' is not undefined. 由于您的代码不会引发未定义的异常,因此我们可以推断出“ msg”并非未定义。

Use a debugger to see what your msg really contains: 使用调试器查看您的味精实际包含的内容:

alert(msg.SalesRep);
console.log(msg);

note: contentType should be 'charset=utf-8', not 'character=utf-8'. 注意:contentType应该是'charset = utf-8',而不是'character = utf-8'。 also the string you're sending is not valid json. 您发送的字符串也不是有效的json。 It should be double quoted instead of using single quotes. 应该使用双引号而不是使用单引号。

So the answer was really simple once I checked the console log. 因此,一旦我检查了控制台日志,答案就非常简单。 I added this line of code and I was able to begin accessing everything in the JSON string: 我添加了以下代码行,就可以开始访问JSON字符串中的所有内容:

var obj = $.parseJSON(msg.d);

This makes no sense: 这没有任何意义:

var strData = "{ strSoldToSelected: '" + strValue + "', strShipToSelected: '" + strShipToVal + "' }";                

It should just be 应该是

var strData = { strSoldToSelected: strValue, strShipToSelected: strShipToVal };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM