0

我需要能够在我的 ASP.NET 应用程序中通过 AJAX 将字符串数组和字符串数组的数组传递给 Web 服务。但我不断得到

{"Message":"Invalid object passed in, : or } expected. (16): { basicInfo:\"[\"TESTCO\",\"TESTCO\",\"PO BOX 0\",\"My City\",\"MyState\",\"My Zip\",\"MyPhone\",\"TEST1 -  TEST, TEST3 - TEST, \",\"11/15/2012\",\"16:36\",\"myname\",\"1202160021\"]\"}","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

我对 JSON 和序列化以及所有这些都不是很了解。所以我真的不知道我做错了什么。

var cvwOther = new Array();
var equipAssign = new Array();
var qAndA = new Array();
var binfo = new Array(storeinfo[2], storeinfo[6], storeinfo[7], storeinfo[8], storeinfo[9],
                              storeinfo[10], storeinfo[11], storeinfo[12], getToday(), getNow(), userinfo[0], call);

function submitCVW(call) {
      db.transaction(function (tx) {
                    tx.executeSql("Select * From CVWOther Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            cvwOther[i] = result.rows.item(i);
                            i++;
                        });
                    }, onSQLError);
                    tx.executeSql("Select * From EquipAssign Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            equipAssign[i] = result.rows.item(i);
                            i++;
                        });
                    }, onSQLError);
                    tx.executeSql("Select * From CVW Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            qAndA[i] = result.rows.item(i);
                            i++;
                        });

                        $.ajax({
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json",
                            url: "MyService.asmx/CVW",
                            data: '{ basicInfo:"' + JSON.stringify(bInfo) + '", cvwOther:"' + JSON.stringify(cvwOther) + '"}',
                            success: function(data) {
                            },
                            error: function(xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                alert(err.Message);
                            }
                        });
                    }, onSQLError);
                });
            }

Web 方法签名

[WebMethod]
public void SubmitCVW(string[] basicInfo, string[][] cvwOther)
4

1 回答 1

0

JSON 和 javascript 文字之间的主要区别在于您的对象属性名称需要被引用:

{
    "attr_1" : "whatever", // VALID JSON
    attr_2 : "something"   // INVALID JSON
}

JSON.stringify()话虽如此,当您拥有可用的方法时,绝对没有理由需要构建 JSON 字符串。相反,构建您想要编码的数据,然后stringify()在其上使用该方法。

// Why do this ...
data: '{ basicInfo:"' + JSON.stringify(bInfo) + '", cvwOther:"' + JSON.stringify(cvwOther) + '"}',

// ... when you can do this?
data: JSON.stringify( {basicInfo:bInfo, cvwOther:cvwOther} );
于 2012-11-16T00:05:25.473 回答