阿贾克斯方法外部类 [英] Ajax method external class

查看:281
本文介绍了阿贾克斯方法外部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是该解决方案继 http://stackoverflow.com/a/4508430/1622376

Following on from this solution http://stackoverflow.com/a/4508430/1622376 .

我试图修改它通过从外部类中使用sayHello方法:

I'm trying to modify it to use the sayhello method from an external class by:

Example2.aspx

Example2.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>
<%@ Register Assembly="MyClasses" Namespace="MyClasses" TagPrefix="test" %>


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<test:Ajax2 runat="server" />

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(function () {
                $.ajax({
                    type: 'POST',
                    url: 'Example2.aspx/sayhello',
                    data: JSON.stringify({ name: 'Scott' }),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (msg) {
                        // Notice that msg.d is used to retrieve the result object
                        alert(msg.d);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.responseText);
                        alert(thrownError);
                    }
                });
            });
        }); 
    </script>

</body>
</html>

然后我的课用的SayHello方法是:

then my class with the sayhello method is:

using System;
using System.Web;
using System.Text;

namespace MyClasses
{

    public class Ajax2 : System.Web.UI.Page
    {

        public static string SayHello(string name)
        {
            return "Hello " + name;
        }

    }

}

我已经添加了Ajax的错误funtion给予为什么请求失败的一些信息:

i've added an the ajax error funtion to give some information on why the request is failing:

<title>Unknown web method sayhello.<br>Parameter name: methodName</title>

[ArgumentException: Unknown web method sayhello.

Parameter name: methodName]

   System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +488515

   System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +162

   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136

   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +69

就是我试图做甚至可能吗?

Is what i'm trying to do even possible?

推荐答案

您必须注解你的方法,因此编译器知道它是一个PageMethod的和jQuery的那么方便。

You must annotate your method so the compiler knows that it is a PageMethod and so accessible from jQuery.

[System.Web.Services.WebMethod]
public static string SayHello(string name) { ... }

转换到ASMX Web服务:

Converting to asmx web service:

[System.Web.Script.Services.ScriptService]
public class Ajax2 : System.Web.Services.WebService
{
    [WebMethod]
    public string SayHello(string name)
    {
        return "Hello " + name; 
    }
}

这篇关于阿贾克斯方法外部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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