I am getting an Invalid JSON Primitive error while posting the data to a local webservice.
stack trace : {"Message":"Invalid JSON primitive: callback.","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.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\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"}
Jquery code on the asp.net page
function SubmitJQueryToWebService() {
$.ajax({
type: "POST",
url: "http://localhost/JSONWebService/Service1.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(msg) {
alert('dsfasdf');
alert(msg.d);
},
error: function(x, y, z) {
alert(x.responseText);
}
});
}
Web Service Method
[WebMethod]
public string HelloWorld()
{
string str = "HelloWorld";
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(str.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, str);
string json = Encoding.Default.GetString(ms.ToArray());
return json;
}
I have also added the [ScriptService] attribute to the web service.
Any help would be appreciated.
Thanks,
Sunil