在asp.net中将json数组发送到Web服务时出错

时间:2014-03-27 11:57:01

标签: asp.net json

我正在尝试将一堆我的IndexedDB数据(表格)作为 JSON发送到我的ASP.NET WebService ..我已经尝试了所有但无法解决它..这是从我的控制台窗口我的错误messafe ..

function Insert(Applicant) {
  $.ajax({ type: "POST", async: false, url: "/WebService1.asmx/Insert",
  //datatype: "text", 
    data: JSON.stringify({ ApplicantList: Applicant }),
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(str) { alert("Message: "+str.d); },
     error: function(err) { alert("Error: "+err.d); } }); 
 }

错误

ExceptionType: "System.InvalidOperationException"
Message: "Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1[IndexedDB.Applicant]'"
StackTrace: "   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Obj
{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1[IndexedDB.Applicant]\u0027","StackTrace":"   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

IndexedDB代码

             cursor.onsuccess = function(e) {
                var result = e.target.result;
                console.log(result);

                if(result) {
                    if(json == "") {
                        json += " [ ";
                    }
                    else {
                        json += ",";
                    }

                    json += " {\"Id\":\""+result.value["id"]+"\", ";
                    json += " \"Applicant_Name\":\""+result.value["AM_Applicant_Name"]+"\", ";
                    json += " \"Father_Name\":\""+result.value["AM_Father_Name"]+"\", ";
                    json += " \"Mobile_No\":\""+result.value["AM_Mobile_No"]+"\", ";
                    json += " \"Address\":\""+result.value["AM_Address"]+"\", ";
                    json += " \"Address1\":\""+result.value["AM_Address1"]+"\", ";
                    json += " \"Address2\":\""+result.value["AM_Address2"]+"\", ";
                    json += " \"Pin_Code\":\""+result.value["AM_Pin_Code"]+"\", ";
                    json += " \"District_Code\":\""+result.value["AM_District_Code"]+"\", ";
                    json += " \"Taluk_Code\":\""+result.value["AM_Taluk_Code"]+"\", ";
                    json += " \"Hobli_Code\":\""+result.value["AM_Hobli_Code"]+"\", ";
                    json += " \"Village_Code\":\""+result.value["AM_Village_Code"]+"\", ";
                    json += " \"Habitation_Code\":\""+result.value["AM_Habitation_Code"]+"\", ";
                    json += " \"Reservation_Code\":\""+result.value["AM_Reservation_Code"]+"\", ";
                    json += " \"Caste_Code\":\""+result.value["AM_Caste_Code"]+"\"} ";

                    result.continue();
                }
            }
            cursor.onerror = function(e) {
                console.log(e.target.error.name);
                alert(e.target.error.name);
            }
            trans.oncomplete = function() {
                if(json != "") {
                    json += "];";
                    //alert(json);
                    Insert(json);
                }
            }

网络服务

[System.Web.Script.Services.ScriptService]

public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public string Insert(List<Applicant> ApplicantList)
    {
        string value = "";
        if (ApplicantList != null)
        {
            foreach (var item in ApplicantList)
            {
                value += item.Applicant_Name + " ";
            }
        }
        return value;
    }
}

我将json值传递给我的网络服务

{"ApplicantList":" [  
 {\"Id\":\"14\",  \"Applicant_Name\":\"arun\",
  \"Father_Name\":\"gopi\",  \"Mobile_No\":\"7875645756\",  \"Address\":\"rter\", 
  \"Address1\":\"werty\",  \"Address2\":\"rter\",  \"Pin_Code\":\"674564\", 
  \"District_Code\":\"8\",  \"Taluk_Code\":\"5\",  \"Hobli_Code\":\"2\", 
  \"Village_Code\":\"642100\",  \"Habitation_Code\":\"0\",  \"Reservation_Code\":\"2\", 
  \"Caste_Code\":\"383\"} , 
 {\"Id\":\"12\",  \"Applicant_Name\":\"asha\", 
  \"Father_Name\":\"qwasha\",  \"Mobile_No\":\"8968567675\",  \"Address\":\"asdfgh\", 
  \"Address1\":\"fjk\",  \"Address2\":\"dfjhk\",  \"Pin_Code\":\"765765\", 
  \"District_Code\":\"9\",  \"Taluk_Code\":\"3\",  \"Hobli_Code\":\"2\",  
  \"Village_Code\":\"668800\",  \"Habitation_Code\":\"0\",  \"Reservation_Code\":\"5\", 
  \"Caste_Code\":\"788\"} , 
 {\"Id\":\"13\",  \"Applicant_Name\":\"asha\", 
  \"Father_Name\":\"mani\",  \"Mobile_No\":\"9757543346\",  \"Address\":\"sdf\", 
  \"Address1\":\"sdfsd\",  \"Address2\":\"gdf\",  \"Pin_Code\":\"565664\", 
  \"District_Code\":\"8\",  \"Taluk_Code\":\"6\",  \"Hobli_Code\":\"1\", 
  \"Village_Code\":\"623600\",  \"Habitation_Code\":\"0\",  \"Reservation_Code\":\"4\", 
  \"Caste_Code\":\"751\"} 
];"}

申请人类

public class Applicant
{
    public Applicant() { }

    public string Applicant_Name { get; set; }
    public string Father_Name { get; set; }
    public string Address { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Mobile_No { get; set; }
    public string Pin_Code { get; set; }
    public string District_Code { get; set; }
    public string Taluk_Code { get; set; }
    public string Hobli_Code { get; set; }
    public string Village_Code { get; set; }
    public string Habitation_Code { get; set; }
    public string Reservation_Code { get; set; }
    public string Caste_Code { get; set; }
    public string Id { get; set; }
}

1 个答案:

答案 0 :(得分:1)

虽然这不是同一个问题,但我并没有将其作为重复报告。但是你可以在here上找到答案,因为你似乎犯了类似的错误。尝试更改js函数和JSON创建,如下所示:

var Applicant = 
[  
 {\"Id\":\"14\",  \"Applicant_Name\":\"arun\",\"Father_Name\":\"gopi\",  \"Mobile_No\":\"7875645756\",  \"Address\":\"rter\", \"Address1\":\"werty\", \"Address2\":\"rter\",  \"Pin_Code\":\"674564\",   \"District_Code\":\"8\",  \"Taluk_Code\":\"5\",  \"Hobli_Code\":\"2\",   \"Village_Code\":\"642100\",  \"Habitation_Code\":\"0\",  \"Reservation_Code\":\"2\",   \"Caste_Code\":\"383\"} 
, 
 {\"Id\":\"12\",  \"Applicant_Name\":\"asha\", \"Father_Name\":\"qwasha\",  \"Mobile_No\":\"8968567675\",  \"Address\":\"asdfgh\", \"Address1\":\"fjk\",  \"Address2\":\"dfjhk\",  \"Pin_Code\":\"765765\", \"District_Code\":\"9\",  \"Taluk_Code\":\"3\",  \"Hobli_Code\":\"2\", \"Village_Code\":\"668800\",  \"Habitation_Code\":\"0\",  \"Reservation_Code\":\"5\", \"Caste_Code\":\"788\"} 
, 
 {\"Id\":\"13\",  \"Applicant_Name\":\"asha\", \"Father_Name\":\"mani\",  \"Mobile_No\":\"9757543346\",  \"Address\":\"sdf\", \"Address1\":\"sdfsd\",  \"Address2\":\"gdf\",  \"Pin_Code\":\"565664\", \"District_Code\":\"8\",  \"Taluk_Code\":\"6\",  \"Hobli_Code\":\"1\", \"Village_Code\":\"623600\",  \"Habitation_Code\":\"0\",  \"Reservation_Code\":\"4\", \"Caste_Code\":\"751\"} 
];


function Insert() {
  $.ajax({ type: "POST", async: false, url: "/WebService1.asmx/Insert",
  //datatype: "text", 
    data: JSON.stringify({ ApplicantList: Applicant }),
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(str) { alert("Message: "+str.d); },
     error: function(err) { alert("Error: "+err.d); } }); 
 }

希望这有帮助。