Click here to Skip to main content
14,736,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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: "plain/text",
        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
Posted
Updated 11-Aug-20 4:07am
v4
Comments
F-ES Sitecore 26-Feb-16 7:10am
   
You probably can't use "#" as a property name.
Richard Deeming 26-Feb-16 8:53am
   
The error suggests there's a mis-match between the JSON you're sending and the structure of the class your WebMethod is trying to deserialize. What does the class look like?

try this code


function TEST() {
        var str =JSON.stringify({

            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);
        }
    });
}
   
Comments
stellus 27-Feb-16 0:46am
   
Thank you mehul,
i tried your code but again i got the different error message ,
please find below my error message and webmethod code,

error:
---------------------------
Message from webpage
---------------------------
{"Message":"Invalid web service call, missing value for parameter: \u0027Object\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
---------------------------
OK
---------------------------


My WebMethod code is:

[WebMethod]
public static string TEST(string Object)
{
return "success";
// return "{data:data}";
}

thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900