开发者

jquery ajax get

Why this doesn't work ? (i tried everything!)

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "Webservice.asmx/HelloWorld",
    data: { param1: "aaa" },
    success: function (msg) {
        alert(msg.d);
    }
});

the web service

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, XmlSerializeString = false, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld(string param1)
{
return "Hello World";
}

the error message

{"Message":"Invalid JSON primitive: aaa.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerialize开发者_JAVA技巧r serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromGetRequest(HttpContext context, JavaScriptSerializer serializer, WebServiceMethodData methodData)\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"}


try making your data a full string like:

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "Webservice.asmx/HelloWorld",
    data: '{ "param1": "aaa" }',
    success: function (msg) {
        alert(msg.d);
    }
});


With ASP.NET, your ajax request has to be POST to use JSON.

$.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);
    }
});

Refer to this for more information.


As an alternative to using the slightly annoying to work with web service projects, I'd suggest looking into asp.net mvc and just use controllers to act as your 'services'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜