简体   繁体   English

Ajax 调用网页方法不起作用

[英]Ajax Call WebPage Method doesn't work

I do我愿意

<script type="text/javascript"  src="Scripts/jquery-1.4.1.min.js"></script>

<script type="text/javascript">
    function CallPageMethod(methodName, onSuccess, onFail) {
        var args = '';
        var l = arguments.length;
        if (l > 3) {
            for (var i = 3; i < l - 1; i += 2) {
                if (args.length != 0) args += ',';
                args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
            }
        }
        var loc = window.location.href;
        loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Report.aspx" : loc;
        $.ajax({
            type: "POST",
            url: loc + "/" + methodName,
            data: "{" + args + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: onSuccess,
            fail: onFail
        });
    }

    function success(response) {
        alert(response.d);
    }

    function fail(response) {
        alert("&#1054;&#1096;&#1080;&#1073;&#1082;&#1072;.");
    }

    function select() {

        ...........

        CallPageMethod("SelectBook", success, fail, ...........);
    }
</script>

And my Script Manager is我的脚本管理器是

<asp:ScriptManager ID="ScriptManager1" 
EnableScriptGlobalization="true" 
EnableScriptLocalization="true"
EnablePageMethods="true" 
EnablePartialRendering="true" runat="server" />

But when I execute select function I got nothing ... no messages at all.但是当我执行 select 函数时,我什么也没得到……根本没有消息。

Error log :错误日志:

    [ArgumentException]: Unknown web method SelectBook.
Parameter name: methodName
   at System.Web.Script.Services.WebServiceData.GetMethodData(String methodName)
   at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

And my web method :还有我的网络方法:

<WebMethod()> Public Function SelectBook(ByVal values As String, ByVal valuesG As String, ByVal valuesX As String, ByVal valuesXG As String) As String
    '....
    Return If(((vals.Length = valsG.Length) And (valsX.Length = valsXG.Length)), SQLModule.UpdateAdvCode(vals, valsG, valsX, valsXG), "Ошибка чтения диспетчерских кодов")
End Function

If I only do alert( CallPageMethod("SelectBook", success, fail, ...........) ) I can see "undefined" , but what undefined ???如果我只做 alert(CallPageMethod("SelectBook",success,fail,.....)) 我可以看到 "undefined" ,但是什么 undefined ??? I was tried to debug this javascript but can't fix it.我试图调试这个 javascript,但无法修复它。 It was worked and I understand how it must works, but I just have no idea why it is broken for now and how to fix it.它奏效了,我明白它必须如何工作,但我只是不知道它为什么现在坏了以及如何修复它。 Help me with it please.请帮我解决。

Some more information更多信息

    Server  ASP.NET Development Server/10.0.0.0
Date    Thu, 03 Feb 2011 11:30:51 GMT
X-AspNet-Version    4.0.30319
Cache-Control   private
Content-Type    text/html; charset=utf-8
Content-Length  3517
Connection  Close

why close ?为什么关闭?

Have you checked the config file, is the Module registered您是否检查过配置文件,是否已注册模块

<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

Problem solved ....问题解决了....

<WebMethod()> Public SHARED Function SelectBook(ByVal values As String, ByVal valuesG As String, ByVal valuesX As String, ByVal valuesXG As String) As String
    '....
    Return If(((vals.Length = valsG.Length) And (valsX.Length = valsXG.Length)), SQLModule.UpdateAdvCode(vals, valsG, valsX, valsXG), "Ошибка чтения диспетчерских кодов")
End Function

I spend 2 days to see it must be Shared >_<我花了2天看到它必须共享>_<

i've got same problem with $.ajax, dunno why but so on i'm now using $.get() or $.getJSON() for my XMLHTTPRequests.我在使用 $.ajax 时遇到了同样的问题,不知道为什么,但我现在正在为我的 XMLHTTPRequests 使用 $.get() 或 $.getJSON()。 seems even faster than with $.ajax() function.似乎比 $.ajax() 函数更快。

I pay so times for finding the right answer but not find in any site .我为找到正确答案而付出了很多时间,但在任何网站上都找不到。 That is a very very simple answer: It is depends on parameter location in development mode and publish mode on internet.这是一个非常非常简单的答案:这取决于开发模式下的参数位置和互联网上的发布模式。 In develop mode your URL is like this Http:// localhost:portnumber/yournamesite/pagesite/parameter And location of your parameter sit on 4 position of url and when publish your site on internet on iis , your URL has become like this Http:// yoursite.com/pagesite/parameter And location of your parameter is on position number 3. ;) Excuse me for bad English speaking ;)在开发模式下,您的 URL 是这样的 Http:// localhost:portnumber/yournamesite/pagesite/parameter 并且您的参数位置位于 url 的 4 个位置,当在 iis 上在 Internet 上发布您的站点时,您的 URL 变成了这样的 Http: // yoursite.com/pagesite/parameter 你的参数的位置在第 3 位。;) 对不起,英语不好;)

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

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