简体   繁体   English

Ajax调用Web方法在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. 我试图在VS 2010中建立一个非常简单的Web方法调用,但失败了。 The exact same setup works in VS2008 and I can't figure out where the difference is. 完全相同的设置可以在VS2008中使用,我不知道区别在哪里。 The code is as follows: 代码如下:

test.aspx ajax call (I also tried without the localhost): test.aspx ajax调用(我也尝试了没有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): test.aspx.cs(断点显示从未达到该方法):

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

web.config (I took those from the VS2008 version that works): 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>

The ready state returned is always 0 and the response text empty; 返回的就绪状态始终为0,响应文本为空; firebug shows the correct data is posted, the response tab shows the page html; firebug显示正确的数据发布,响应选项卡显示html页面; I've not had to use the firebug debugger before, so perhaps there's something more I should look at. 以前我不必使用Firebug调试器,所以也许还有更多我需要研究的东西。 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. 也许有一个事实可能是,我最初尝试实现WCF服务,但由于“身份验证失败”而一直失败-从我读到的内容到IIS安装,但发现的所有帖子均无效。 Since it does not have to be WCF, I eventually thought I go back to basics, ie this simple web method. 由于不必一定是WCF,所以我最终认为我可以回到基础知识,即这种简单的Web方法。 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. 让我认为这不是VS2008版本与完全相同的服务器一起工作的事实。

Any suggestions are massively appreciated. 任何建议都受到高度赞赏。

got it to work... finally... posting it as an answer in case someone else runs into this 让它起作用...最后...将其发布为答案,以防其他人遇到该问题

the problem was the call needs async: false, or - at least in my case - it fails on most browsers; 问题是调用需要异步:错误,或者-至少在我看来-在大多数浏览器中失败; it doesn't make sense to me though as to why, so i'll post a separate question, hopefully someone can explain 对于我来说,这没有什么意义,所以我将发布一个单独的问题,希望有人可以解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM