Jquery ajax get

jquery

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, 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.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"}

Best Answer

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);
    }
});
Related Topic