Ajax.Request, parameterrs, JSON, and ASP.NET - Invalid JSON primitive

3,749 views
Skip to first unread message

Maulkye

unread,
Sep 3, 2008, 8:31:04 PM9/3/08
to Prototype & script.aculo.us
Hello all,

I keep getting this error message (Invalid JSON primitive) when I try
to call an Ajax.Request with the 'applicatio/json' content type. I've
tried formatting the parameters in a variety of ways, but no luck.
Can anyone help?

Here's what I have:

CLIENT:
new Ajax.Request("/myService.asmx/GetComplexData",{
onSuccess: onSuccess,
onFailure: onFailure,
method:"post",
contentType:"application/json; charset=utf-8",
parameters: {s: 'bob'}
});


SERVER:

[WebMethod]
public string GetComplexData(string s)
{
return s;
}



Note that other calls are working. If I remove the content type, this
woks ok, but it just populates the string parameter with the value of
"s" from the client. Additionally, I tried this on the server, but to
no avail:

[WebMethod]
public string GetComplexData(object s)
{
return "ok";
}


It seems like there's some way I'm supposed to format the parameters
to make MS.NET happy, but I can't figure out how it is. The server
can obviously handle it, because if I use their big fat client library
it sends json objects over and puts them into a dictionary object
without a hitch.

If anyone can offer some suggestions of even point in the right
direction, I would be most appreciative.

Thanks,
-M

Full Error Message:
-----------------------------
{"Message":"Invalid JSON primitive: s.","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"}

david

unread,
Sep 10, 2008, 6:44:12 AM9/10/08
to Prototype & script.aculo.us
Hi Maulkye,

You set the application/json in the client call, but it is the
response from the server that should send this info. Is it the case?
Next, It seems that the JSON is not very well formatted, asOK is not a
good JSON value, try a good JSON ?

And my last remark is that it seems to be a server side problem ?? not
a real PROTOTYPE problem

--
david.

T.J. Crowder

unread,
Sep 10, 2008, 8:21:45 AM9/10/08
to Prototype & script.aculo.us
Hi Maulkye,

Just off the top of my head, does your JSON looks like this:

{
thingy: "value"
}

or like this:

{
"thingy": "value"
}

The former is not valid JSON syntax. It is valid JavaScript object
literal syntax, but not JSON, which is a _subset_ of object literal
syntax. The only primitive values (e.g., things you leave out of
quotes) in JSON are false, true, and null.

So if you're not putting quotes around the things on the left, you
might try that to see if it's the problem. Maybe the MS parser is
being picky.

HTH,
--
T.J. Crowder
tj / crowder software / com

Maulkye

unread,
Sep 10, 2008, 7:47:02 PM9/10/08
to Prototype & script.aculo.us
Hi and thanks for you response,

I have been having trouble with google not posting my messages, so I
accidentally posted this issue twice. Additionally, the lengthy
solution I posted to the porblem disappeared as well. I don't know
why, but I'd rather not post it again.

At any rate, you are correct in that I just never got the JSON format
right. In the end, the biggest error I made was not including the
parameter in the JSON:

This format proved successful worked:

'{"p_Person":
{"__type":"WebServiceCalls.Person","firstName":"John","lastName":"Smith","greeting":"Hello"}}'


Thanks,
Maulkye


On Sep 10, 8:21 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> HiMaulkye,
>
> Just off the top of my head, does your JSON looks like this:
>
>     {
>         thingy:  "value"
>     }
>
> or like this:
>
>     {
>         "thingy":  "value"
>     }
>
> The former is not valid JSON syntax.  It is valid JavaScript object
> literal syntax, but not JSON, which is a _subset_ of object literal
> syntax.  The only primitive values (e.g., things you leave out of
> quotes) in JSON are false, true, and null.
>
> So if you're not putting quotes around the things on the left, you
> might try that to see if it's the problem.  Maybe the MS parser is
> being picky.
>
> HTH,
> --
> T.J. Crowder
> tj / crowder software / com
>
> > System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePri­mitiveObject()
> > \r\n at
> > System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInt­ernal(Int32
> > depth)\r\n at
> > System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeseriali­ze(String
> > input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at
> > System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScript­Serializer
> > 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(HttpCont­ext
> > 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"}- Hide quoted text -
>
> - Show quoted text -

boblo

unread,
Oct 9, 2008, 2:13:09 PM10/9/08
to Prototype & script.aculo.us
The Object.toJSON(obiect) method can be used to serialize an object
into JSON format.
Reply all
Reply to author
Forward
0 new messages