jquery ajax get
为什么这不起作用? (我尝试了一切!)
1 2 3 4 5 6 7 8 9 | $.ajax({ type:"GET", contentType:"application/json; charset=utf-8", url:"Webservice.asmx/HelloWorld", data: { param1:"aaa" }, success: function (msg) { alert(msg.d); } }); |
网络服务
1 2 3 4 5 6 | [WebMethod] [ScriptMethod(UseHttpGet = true, XmlSerializeString = false, ResponseFormat = ResponseFormat.Json)] public string HelloWorld(string param1) { return"Hello World"; } |
错误消息
{"Message":"无效的JSON原语:aaa。","StackTrace":"在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal上的System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() r n" System.Web.Script.Serialization.JavaScript序列化程序,字符串处于System.Web.Script.Serialization.JavaScript序列化程序,字符串处理器中的System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串输入,Int32 depthLimit,JavaScriptSerializer序列化程序) r n的Int32深度) r n System.Web.Script.Script.Services.RestHandler.GetRawParams中的System.Web.Script.Services.RestHandler.GetRawParamsFromGetRequest(HttpContext上下文,JavaScriptSerializer序列化程序,WebServiceMethodData methodData) r n的输入,类型类型,Int32 depthLimit) r n (WebServiceMethodData methodData,HttpContext context) r n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
使用ASP.NET,您的ajax请求必须是POST才能使用JSON。
1 2 3 4 5 6 7 8 9 10 | $.ajax({ // type:"GET", type:"POST", contentType:"application/json; charset=utf-8", url:"Webservice.asmx/HelloWorld", data: '{ param1: value1, param2: value2 }', success: function (msg) { alert(msg.d); } }); |
有关更多信息,请参阅此处。
尝试使您的数据像一个完整的字符串:
1 2 3 4 5 6 7 8 9 | $.ajax({ type:"GET", contentType:"application/json; charset=utf-8", url:"Webservice.asmx/HelloWorld", data: '{"param1":"aaa" }', success: function (msg) { alert(msg.d); } }); |
作为使用稍微烦人的Web服务项目的替代方案,我建议查看asp.net mvc并使用控制器充当你的"服务"。