實際上,我試圖學習對asp.Net webservices的jQuery Ajax調用。關於此代碼的幫助,它試圖:對需要返回JSON的Web服務進行jQuery Ajax調用
我一直在試圖撥打下面的WebMethod:
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是這樣的:
<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);
};
</script>
與一個JSON相關回應說結果:
{「消息「:」無效的JSON基元:名字。「,」StackTrace「:」System.Web.Script.Serialization.Java上System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\ r \ n在System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串輸入,Int32 depthLimit,JavaScriptSerializer序列化程序)上的ScriptObjectDeserializer.DeserializeInternal(Int32深度)\ r \ n \ r \ n System.Web.Script.Serialization.JavaScriptSerializer.Deserialize JavaScriptSerializer序列化程序,字符串輸入,類型類型,Int32 depthLimit)\ System.Web.Script.Services.RestHandler上System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](String input)\ r \ n \ r \ n。 GetRawParamsFromPostRequest(HttpContext的上下文中,JavaScriptSerializer串行個)\ r \ n在System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext的上下文)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的上下文,WebServiceMethodData methodData)「,」ExceptionType「:」System.ArgumentException「}
我是否需要序列化我的在設置'data'屬性之前以某種方式設置對象,以便我可以調用$ .ajax()函數?
感謝
我試圖通過刪除DateTime屬性,它的工作。我將致力於如何使其工作。謝謝ckramer。另外,感謝@patrick爲stringify建議。 – pencilCake 2010-06-20 00:05:56
太棒了,很高興我能幫到你。 – ckramer 2010-06-20 00:15:55