"消息":"传入无效对象(JSON数据格式问题)

时间:2015-05-28 05:04:22

标签: javascript json webmethod

我正在尝试将json发送到我的webmethod,但它显示在下面错误。

{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (17): { \"mappingData
:\"\"[\"Ref No,0\",\"Date,0\",\"Amt,0\",\"Sender Name,0\",\"Sender Add,0\",\"Beneficiary Name,0\",\"Beneficiary
 Add,0\",\"Phone,0\",\"Secret Code,0\",\"Secret Ans,0\",\"Preferred Id,0\"]\"}","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
(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script
.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services
.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n  
 at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext
 context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,
 WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

我的Javascript代码是:

    function tableToJson() { 
        var myArray = [];
        $('.dataRow').each(function(){
            var data= $(this);
            var ColumnAlias= data.find('div.ColumnAlias p').html();
            var TableNames=data.find('div.ddlTableNames select').val();
            var mainData=ColumnAlias+','+TableNames;
            myArray.push(mainData);

        });
        var json_text = JSON.stringify(myArray);
        if (json_text!=null) {
            $.ajax({
                type: "POST",
                url: "CorrespondentTemplate.aspx/SaveMappings",
                data: '{ "mappingData:""'+json_text+'"}',
                contentType: "application/json",
                success: function (data) {
                    alert(data.d);
                }
            });
        }

        return false;
    }

请检查此行,我不知道它有什么问题

data: '{ "mappingData:""'+json_text+'"}',

2 个答案:

答案 0 :(得分:1)

行情错位。应该是

bin

或者你可以这样做

data: '{ "mappingData":"'+json_text+'"}',

答案 1 :(得分:-1)

我尝试了以下方法来解决上述问题。

id  server_counts   isalive
1         0            1

这三行完成了我的工作。

    function tableToJson() { 
        var myArray = [];
        $('.dataRow').each(function(){
            var data= $(this);
            var ColumnAlias= data.find('div.ColumnAlias p').html();
            var TableNames=data.find('div.ddlTableNames select').val();
            var mainData=ColumnAlias+','+TableNames;
            myArray.push(mainData);

        });
        var json_text = JSON.stringify(myArray);
        var jsonFormat={mappingData:json_text};
        var jsonFormatInString=JSON.stringify(jsonFormat);
        if (json_text!=null) {
            $.ajax({
                type: "POST",
                url: "CorrespondentTemplate.aspx/SaveMappings",
                data: jsonFormatInString,
                contentType: "application/json",
                success: function (data) {
                    alert(data.d);
                }
            });
        }

        return false;
    }