views:

190

answers:

3

I am trying to send JSON from my mozilla addon to my asp.net page.

var myJSONObject = {"userName": una,"password": pass}; request = new XMLHttpRequest(); request.open("GET","http://www.google.com?jo=" + myJSONObject,true, null, null);

on my .net page I have tried several ways of doing it but not able to find the best way to serialize and deserialize the code.

All I need is to send the json data back n forth and parse it on C# n javascript.

I have tried DataContractJsonSerializer, JavaScriptSerilizer among many other things. But not able to make any of it work. With the JavaScriptSerilizer, It does deserilize it if it takes an argument from the browser for e.g. If I open up the browser and paste something like http://www.google.com?jo={"Username":"hna123","Password":"2444"} it does deserilize and return me individual values, but it does work when I do an XMLHTTPRequest (as above) from my mozilla addon. Any clues?

A: 

If you want to pass data as querystring then you need to append that to the url using ?.

Try changing the request url from

request.open("GET",http://www.google.com/jo=" + myJSONObject

to

request.open("GET",http://www.google.com?jo=" + myJSONObject

and the use Request.QueryString collection to get the value in C# like

Request.QueryString["jo"]
rahul
Ah sorry I missed it while pasting the code here..
Hna0002
Request.QueryString will give me as a string and will not allow me to access individual elements of the json.
Hna0002
A: 

You need to parse the QueryString yourself to an object in the code behind file to access individual elements of the json.

John Hpa
A: 

Ah, I was missing JSON.stringify(). Thanks anyway.

Hna0002