I suggest that if you are already encased in something as heavy as dotNet, you should use its internal power
- Make an
<input type="hidden" runat="server" ...> object (or its <asp:...> equivalent)
- have the page code keep its value up-to-date, and then
- get the value out of that "hidden" in your JavaScript.
If you need to control when the value gets updated use:
- __doPostback(), or
- ClientScriptManager.GetPostBackEventReference.
Those wrappers let dotNet write the JavaScript that calls an event handler that will update the variable for you. That code will be more able to avoid running afoul of other dotNet functionality on the same page.
Adding two post-back mechanisms to the same page is asking for weird interactions that will be hard to debug, especially since you seem to misunderstand ajax in a fundamental way.
Ajax submits the entire page to your code and returns the output. So unless you have a path through your page code that notices the marker on the query string and outputs the stringified value of the variable, this will not work.
Put a breakpoint in Page_Load and you will see the ajax calls back into your page as though it were preparing another copy of the entire page.
You have the option at that point of noticing the decoration that indicates this is AJAX, diverting the page preparation and writing your variable contents to the Response, and then skipping further processing.
It is better to prepare an entirely different URL for Ajax to use, in which case you should use WebServices as noted by an earlier respondent.
But, again. There is a way to do this entirely in dotNet, and it is already using these mechanisms. So your attempts to use the same ones can become very hard to debug when they go wrong.
So I would either avoid Ajax, or avoid dotNet, on any given page.
getDataNodes()and then return the list from there. Call that method in your ajax call.