Ajax调用Web方法在VS2010不工作 [英] ajax call to web method not working in VS2010

查看:271
本文介绍了Ajax调用Web方法在VS2010不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个非常简单的Web方法调用在VS 2010中,并惨遭失败。完全相同的安装工作在VS2008,我无法揣摩出的差异。在code是如下:

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调用(我也试过没有本地主机):

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(断点显示永远达不到法):

test.aspx.cs (breakpoint shows the method is never reached):

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

的web.config(我把那些从VS2008版本的作品):

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>

就绪状态返回的始终是0和响应文本空的;萤火显示了正确的数据发布,响应标签显示HTML页面;我已经没有之前用Firebug调试器,因此,或许有更多的东西我应该看看。控制台/净已启用,但我没有看到任何昭然若揭。

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

也许有指针可能是我最初想实现一个WCF服务,但它一直以身份验证失败失败的事实 - 从我读它与IIS安装,但没有发现我的工作职位。因为它并不一定要WCF,我最终想我回到基础,即这个简单的Web方法。我不知道我怎么能验证它是否是一个身份验证问题或别的东西。是什么让我觉得这是不是就是VS2008版本的作品具有完全相同的服务器的事实。

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.

任何建议都大量AP preciated。

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

这篇关于Ajax调用Web方法在VS2010不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆