ajax调用web方法在VS2010中无效

时间:2013-09-01 08:16:47

标签: c# asp.net ajax visual-studio-2010 web-services

我正在尝试在VS 2010中设置一个非常简单的Web方法调用,并且失败了。完全相同的设置在VS2008中工作,我无法弄清楚差异在哪里。代码如下:

test.aspx ajax call(我也试过没有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(断点显示永远不会达到该方法):

    [WebMethod()]
    public static string Test(string test)
    {
        return "xyz";
    }

web.config(我从那个有效的VS2008版本中获取了那些):

<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>

返回的就绪状态始终为0,响应文本为空; firebug显示正确的数据发布,响应选项卡显示页面html;我以前不必使用firebug调试器,所以也许我应该看一些更多的内容。控制台/网络已启用,但我没有看到任何明显的东西。

我搜索了很多,但找不到任何有帮助或我没有尝试过的东西。总之,我不知道还能在哪里看......

也许一个指针可能是我最初尝试实现WCF服务的事实,但是它仍然因“身份验证失败”而失败 - 从我读到的内容到IIS设置,但我找到的帖子都没有。由于它不一定是WCF,我最终还是认为我会回到基础,即这个简单的Web方法。我不知道如何验证它是否是身份验证问题或其他问题。是什么让我觉得这不是VS2008版本与完全相同的服务器一起工作的事实。

任何建议都受到了高度赞赏。

1 个答案:

答案 0 :(得分:0)

让它发挥作用......最后......发布它作为答案以防其他人遇到此

问题是呼叫需要async:false,或者 - 至少在我的情况下 - 它在大多数浏览器上都失败了;关于为什么,这对我没有意义,所以我会发一个单独的问题,希望有人可以解释