JSON parsing in webservice and returning a value
I am working on a webservice and a jQuery AJAX. It would take a modal dialog and get the inputs from the modal dialog put it into an object and then return a boolean result back from the webservice to the jQuery AJAX call.
Here is the code:
$('[id*="dialog"]').dialog({
autoOpen: false,
modal: true,
resizable: false,
show: "blind",
hide: "puff",
buttons: {
'Create': function() {
Materials.MaterialName = $('[id*="txtMaterialName"]').val();
Materials.QuantityType = $('[id*="txtquantity"]').val();
var jsonMaterial = JSON.stringify(Materials);
AddNewMaterial(jsonMaterial);
$(this).dialog('close');
},
'Cancel': function() {
$(this).dialog('close');
}
},
close: function() {
window.document.location = "ResourcePlanningMaster.aspx";
}
});
function AddNewMaterial(materials) {
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Services/Service.asmx/AddNewMaterial',
data: '{"Materials":"' + materials + '"}',
dataType: "json",
success: function(data, textStatus) {
if (textStatus == "success") {
if (data.d == true) {
alert('New Item Added');
}
}
},
error: function(data, textStatus) {
alert('An error has occured retrieving data!');
}
});
}
and the we开发者_运维百科b service is as :
[WebMethod]
[ScriptMethod]
public String AddNewMaterial(String Materials)
{
Boolean Result = false;
try
{
MaterialEntity Material = JsonConvert.DeserializeObject<MaterialEntity>(Materials);
Result = JsonHelper.InsertMaterial(Material);
}
catch (Exception ex)
{
logger.Error(ex.Message);
logger.Fatal(ex.InnerException);
}
return Result.ToString();
}
the MaterialEntity Class :
public class MaterialEntity
{
public String MaterialName { get; set; }
public String QuantityType { get; set; }
}
Now when I am invking the create button in web service then the error I am getting in data.responseText is :
{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (17): {\"Materials\":\"{\"MaterialName\":\"assd\",\"QuantityType\":\"asd\"}\"}","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"}
Why am I getting this, even though I am parsing it quite correct as a json and then deserializing it as MaterialEntity Class itself? What's causing this error?
Try to use next code
data: '{"Materials":' + materials + '}',
instead of data line in your example (without double quotes around MaterialEntity object).
精彩评论