Ajax调用网页方法不起作用。

时间:2022-05-21 12:22:38

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 ??? I was tried to debug this javascript but can't fix it. 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.

如果我只做提醒(CallPageMethod(“SelectBook”,success, fail,…)),我可以看到“未定义”,但什么是未定义的?我曾试图调试这个javascript,但无法修复它。它是有用的,我理解它是如何工作的,但我只是不知道它为什么现在被打破,以及如何修复它。请帮我一下。

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 ?

为什么关闭?

3 个解决方案

#1


4  

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天时间看它必须是>_<

#2


3  

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>

#3


0  

i've got same problem with $.ajax, dunno why but so on i'm now using $.get() or $.getJSON() for my XMLHTTPRequests. seems even faster than with $.ajax() function.

我也有同样的问题。我现在使用$.get()或$. getjson()来获取xmlhttprequest。似乎比使用$.ajax()函数更快。

#1


4  

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天时间看它必须是>_<

#2


3  

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>

#3


0  

i've got same problem with $.ajax, dunno why but so on i'm now using $.get() or $.getJSON() for my XMLHTTPRequests. seems even faster than with $.ajax() function.

我也有同样的问题。我现在使用$.get()或$. getjson()来获取xmlhttprequest。似乎比使用$.ajax()函数更快。