2014-03-27 109 views
0

我想向我的ASP.NET WebService發送一堆我的IndexedDB數據(表)作爲JSON ..我試了一切,但無法解決它..這裏是從我的控制檯窗口中我的錯誤messafe ..在asp.net發送json數組到web服務時出錯

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); 
       } 
      } 

Web服務

[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值到我的web服務

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

需要在這裏看到您的代碼 – Rex

+1

而不是在評論中發佈代碼嘗試編輯問題本身,也是根據我的最佳知識,您不能從json發佈列表 – Rex

+0

如何在我的WebService中獲取我的json對象?哪個數據類型對於那個是正確的? –

回答

0

雖然這不是同一個問題,所以我不報告它是重複的。但你可以在這個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); } }); 
} 

希望這會有所幫助。

+0

我的編碼也是一樣的你的帖子..但爲什麼我仍然得到錯誤.. –

+0

Inser函數如何得到申請人,請僅通過更新您的問題來發布代碼。 –

+0

Hi @ShashankChaturvedi,現在我已經添加了我的代碼,現在通過它並糾正我的錯誤... –