Using jQuery with ASP.NET 2.0

CompartilheShare on Facebook0Tweet about this on TwitterEmail this to someone

It all began with

SYNTAXERROR: UNEXPECTED TOKEN <

I’m working on an old system (ASP.NET 2.0) and getting a bunch of problems to solve, like “how do I use jQuery to update fields by calling code-behind methods?”. This is a piece of cake in ASP.NET 3.5, so I figured out it wouldn’t be a great trouble for ASP.NET’s previous version (2.0). I was wrong.

I’ve searched through many (and more) references to try and figure out what was the problem with my JQuery + Ajax + ASP.NET 2.0 page (without ScriptManager). My great problem was the code being returned from the ASP.NET page’s method was XML and not JSON as the ajax request was expecting. So everytime I tried a JSON request, I got that error. Whenever I changed to html or xml output, everything works fine (though I have to filter through the structure to get whatever I want).

The ScriptMethodAttribute is unsupported in ASP.NET versions prior to 3.5, so we’re left with our hands tied.

I have installed ASP.NET Ajax 1.0 Extensions to try and use them. After registering them in the GAC, I tried once again using the JSON format with a simple web service which returns a string (that simple). Soon I realized it would be easier to just stick with the 3.5 version and force and update to the web server. Well. Even then I couldn’t call WebMethods on aspx pages. Until I figured out why.

The WebMethod MUST BE DECLARED AS STATIC

And Dave explains why in a very complete article.

Some of my references include the esquisite series of articles by Dave Ward and the ASP.NET Ajax: