Hi all,
I am having trouble sending JSON to a WebMethod. Here is the way that I am trying to do it.
If there is a better way to do this please let me know.
What I am trying to do is save the JSON object off in a database.
<script type="text/javascript">
function TEST() {
var str = {
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple"},
{ "id" : "ajson2", "parent" : "#", "text" : "Root"},
]
}
$.ajax({
type: "POST",
url: "Default3.aspx/Test",
data: str,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function(e) {
alert(e.responseText);
}
});
}
</script>
Code behind:
[WebMethod]
public static string Test(string Object)
{
return "success";
// return "{data:data}";
}
i got the below error message,
---------------------------
Message from webpage
---------------------------
{"Message":"Invalid JSON primitive: data.","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"}
---------------------------
OK
---------------------------
thanks in advance.
What I have tried:
Error sending JSON to WebMethod