Reputation: 561
I am trying to send a bunch of my IndexedDB datas(Tables) as JSON
to my ASP.NET WebService.. I ve tried everything but couldnt resolve it.. Here is my error messafe from my console Window..
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); } });
}
Error
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 Code
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 Service
[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;
}
}
I am passing below json value to my web service
{"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\"}
];"}
Applicant Class
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; }
}
Upvotes: 0
Views: 632
Reputation: 2793
Although this is not the same question so I am not reportin it as a duplicate. But you will be able to find your answer on this here as you seem to be making the similar mistake. Try changing your js function and JSON creation as following:
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); } });
}
Hope this helps.
Upvotes: 1