0

AJAX 要求を使用して、C# で記述された単純なサーバー側の HelloWorld メソッドを呼び出そうとしています。

デフォルト.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title> 
    <script type ="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript">
        function AjaxCall() {
            $.ajax(
            {
                type: 'post',
                url: 'Default.aspx/Server_HelloWorld',
                datatype: 'json',
                success: function (result) { alert(result); }
            })

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="Scriptmanager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
        <asp:Services>
            <asp:ServiceReference Path="TradeService.asmx" />
        </asp:Services>
    </asp:ScriptManager>
    <button id="Button2" type="button" runat="server"  onclick="AjaxCall()">AJAX+JQuery version</button>

    </form>
</body>
</html>

Default.aspx.cs

namespace AJAXService
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }  
        public String Server_HelloWorld()
        {
            return "Hello, How are you?";
        }
     }
}

ただし、「こんにちは、お元気ですか?」という文字列を返す代わりに、Web ページの html コードを返します。なぜこれが起こるのか誰か知っていますか?私は最終的に、AJAX の部分的なポストバック機能を利用して、サーバー側のメソッドが GridView セルを埋めるために使用できる文字列を返すようにしようとしています。

4

3 に答える 3

1

これを試して

[WebMethod]
public static String Server_HelloWorld()
{
     return "Hello, How are you?";
}

したがって、WebMethod と static を使用します。

于 2013-06-18T18:50:57.447 に答える