Help on this code which tries to: Make a jQuery Ajax call to a webservice that needs to return JSON
实际上,我正在尝试学习对ASP.NET WebServices的jQueryAjax调用。
我尝试调用下面的WebMethod:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class Person { public string FirstName { get; set; } public string Id { get; set; } public string LastName { get; set; } public string Department { get; set;} public DateTime Birthday { get; set; } } public class PersonWebServices : System.Web.Services.WebService { [WebMethod] [ScriptMethod] public Person GetPerson(Person personToCompare) { return GetPerson(personToCompare.Id); } [WebMethod] public Person GetPerson(string Id) { if (string.IsNullOrEmpty(Id)) return null; Id = Id.Trim(); return PersonCollection.GetCachedPersonList().Find(personToCompare => personToCompare.Id == Id); } } |
我的javascript是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <script type="text/javascript"> function loadPerson(txtPersonId){ if(!txtPersonId.length || !(txtPersonId.val().length)){ return; } var ajaxCallOptions = { type:"POST", contentType:"application/json; charset=utf-8", url:"/JQuery/Chapter15-AJAX/PersonWebServices.asmx/GetPerson", data: {FirstName:'',LastName:'', Id:txtPersonId.valueOf(),Birthday:'',Department:''}, dataType:"json", success: function(msg){ alert('done'); }, error: function(msg){ alert('Error: '+msg.status); } } jQuery.ajax(ajaxCallOptions); }; |
结果是一个与JSON相关的响应说:
"message":"无效的json基元:firstname.","stacktrace":"at system.web.script.serialization.javascriptObjectDeserializer.DeserializePrimitive Object()
at system.web.script.serialization.javascriptObjectDeserializer.DeserializeInternal(int32 depth):r at system.web.script.serialization.javascriptObjectDeserializer.basicDeserialize(s)string input,int32 depthlimit,javascriptserializer serializer),位于system.web.script.serialization.javascriptserializer.deserialize(javascriptserializer serializer,string input,type,int32 depthlimit),位于system.web.script.serialization.javascriptserializer.deserialize[t](string input),位于system.web.script.services.resthandler.getrawParamsfromPostRequest(httpContext Context,JavaScriptSerializer序列化程序),位于system.web.script.services.resthandler.getrawParams(webserviceMethodData methodData,httpContext Context),位于system.web.script.services.resthandler.executeWebServiceCall(httpContext Context,webserviceMethodData methodData)","exceptionType":"system.argumentexception"
在设置"data"属性之前,是否需要以某种方式序列化对象,以便调用$.ajax()函数?
谢谢
您可能需要将对象转换为有效的JSON,而不是JavaScript对象。
您可以通过调用
1 | data: JSON.stringify({FirstName:'',LastName:'', Id:txtPersonId.valueOf(),Birthday:'',Department:''}), |
会变成:
1 | {FirstName:'',LastName:'', Id:txtPersonId.valueOf(),Birthday:'',Department:''} |
进入:
1 | '{"FirstName":"","LastName":"","Id":"somevalue","Birthday":"","Department":""}' |
并非所有的浏览器都支持JSON,所以您需要包含JSON2库。
https://github.com/douglascorkford/json-js/blob/master/json2.js
基于错误,您发送到调用中的JSON数据似乎有问题。我不是100%确定,但错误似乎表明它是在试图反序列化一个int时发生的,所以导致问题的要么是该日期时间的反序列化,要么是该日期时间的反序列化。出现问题的原因是,如果是空字符串或空/未定义,则反序列化程序将无法为其赋值。
您可以使int和datetime值可以为空(int?日期时间?)在Person类中,如果服务调用接收到的数据中不存在值,则允许反序列化程序将空值分配给这些值。另一个选项是在用Ajax调用发送JSON数据之前,在JavaScript端分配一些有效的默认值(如0和1/1/1800)。