Sunday, 1 September 2013

ajax call to web method not working in VS2010

ajax call to web method not working in VS2010

I'm trying to set up a very simple web method call in VS 2010, and failing
miserably. The exact same setup works in VS2008 and I can't figure out
where the difference is. The code is as follows:
test.aspx ajax call (I also tried without the localhost):
$(document).ready(function ()
{
$("#btnAjax").click(function ()
{
var testData = "test";
$.ajax({
type: "POST",
url: "http://localhost/test.aspx/Test",
data: JSON.stringify({ test: testData }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg)
{
alert("ok");
},
error: function (xhr, err)
{
alert("readyState: " + xhr.readyState + "\nstatus: " +
xhr.status);
alert("responseText: " + xhr.responseText);
}
})
});
});
test.aspx.cs (breakpoint shows the method is never reached):
[WebMethod()]
public static string Test(string test)
{
return "xyz";
}
web.config (I took those from the VS2008 version that works):
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx"
preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*"
path="*_AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode"
verb="GET,HEAD" path="ScriptResource.axd"
type="System.Web.Handlers.ScriptResourceHandler,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
</handlers>
The ready state returned is always 0 and the response text empty; firebug
shows the correct data is posted, the response tab shows the page html;
I've not had to use the firebug debugger before, so perhaps there's
something more I should look at. Console/Net are enabled but I did not see
anything glaringly obvious.
I searched a lot but can't find anything that helps or that I haven't
tried already. In a word, I don't know where else to look...
Perhaps one pointer might be the fact that I initially tried to implement
a WCF service but it kept failing with 'authentication failed' - from what
I read it has to with IIS setup, but none of the posts I found worked.
Since it does not have to be WCF, I eventually thought I go back to
basics, i.e. this simple web method. I don't know how I could verify if
it's an authentication issue or something else. What makes me think this
is not is the fact that the VS2008 version works with the exact same
server.
Any suggestions are massively appreciated.

No comments:

Post a Comment