javascript - ajax 客户端-服务器通信

标签 javascript asp.net ajax

我之前在这个网站上问过一个关于 ajax 客户端-服务器通信的问题。

我得到了很多帮助。但还是不明白,所以重新提问。

我正在尝试将存储在变量“mem_ID”中的值从我的 javascript 页面...Default.aspx 发送到我的服务器端 - Default.aspx.cs 页面。

Javascript:-

<asp:Button ID="Button6" runat="server" BackColor="Silver" 
            onclientclick="store_memID()" style="margin-left: 20px" Text="Submit" 
            Width="102px" Font-Bold="True" Height="28px" />


<script type = "text/javascript" src ="http://code.jquery.com/jquery-1.9.0.min.js"> </script>


 <script type = "text/javascript"> 

        // Function to caputure client-input- Member_ID.
        function store_memID() {

            // 'TextBox3' is the server side ID. To get the client side ID we do the following:-
            mem_ID = document.getElementById('<%=TextBox3.ClientID%>').value;

            //return confirm('TimeLine is displayed for: ' + mem_ID);

            ajax();
        }


        function ajax(){
            $.ajax({
                url: 'Default.aspx/MyMethod',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ memID: mem_ID }),
                success: function (result) {
                alert(result.d);
                }
            });

        }

        </script>

服务器端:-

    public partial class _Default : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

        }


        // Defining a page method. 
        [WebMethod]
        public static string MyMethod(string mem_ID)
        {
            return string.Format("Thanks for calling me with id: " + mem_ID);



        }
` more code here`....

但是,我仍然没有收到服务器的任何回复。我期待从服务器端返回“感谢您用 ID 给我打电话:.....”。有什么想法吗?

我在MyMethod、响应行、服务器端添加了一个断点,但没有命中。所以我假设这条线甚至没有被遍历。

我是 Asp.net 和 Ajax 的新手。并需要有关此主题的帮助。

最佳答案

<form id="form1" runat="server">
    <div>
    <asp:Button ID="Button6" runat="server" BackColor="Silver" 
            onclientclick="store_memID()" style="margin-left: 20px" Text="Submit" 
            Width="102px" Font-Bold="True" Height="28px" /> 
<script type = "text/javascript" src ="http://code.jquery.com/jquery-1.9.0.min.js"> </script>
 <script type = "text/javascript">

     // Function to caputure client-input- Member_ID.
     function store_memID() {
         var mem_ID;
         // 'TextBox3' is the server side ID. To get the client side ID we do the following:-
         mem_ID = document.getElementById('<%=TextBox3.ClientID%>').value;

         //return confirm('TimeLine is displayed for: ' + mem_ID);

         ajax(mem_ID);
     }
     function ajax(mem_ID) {
         alert(mem_ID);
         $.ajax({
             url: 'Default.aspx/MyMethod',
             type: 'POST',
             contentType: 'application/json; charset=utf-8',
             data: JSON.stringify({ memID: mem_ID }),
             success: function (result) {
                 alert(result.d);
             }
         });
     }
        </script>
    <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
    </div>
    </form>
In Your Code Behind

[WebMethod]
        public static string MyMethod(string memID)
        {
            return string.Format("Thanks for calling me with id: " + memID); 
        }

The method u have done all was right execpt,
The data passing from ajax should match the string Element declared in the Web-method.

关于javascript - ajax 客户端-服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14697281/

相关文章:

jquery - rails 3 : jquery to change partials

PHP 或 JavaScript : no element found error

javascript - 如何在javascript中获取放置在中继器内的特定标签的文本

javascript - 在 AngularJS 应用程序中从服务器检索数据很慢

asp.net - 创建 Web 应用程序以扫描文档

javascript - 使用 jQuery 连续向左滑动动画

javascript - 无法检索 POST 数据

javascript - 取消光谱拾色器不工作

javascript - 当单选按钮值更改时,如何避免将值推送到数组

html - Visual Studio 尝试将 HTML 页面转换为 ASP.NET