【问题标题】:Ajax arguments not been seen by ASP.NETASP.NET 没有看到 Ajax 参数
【发布时间】:2010-02-22 04:19:12
【问题描述】:

我将 Ajax 参数从源 ASP.NET 页面传递到目标 ASP.NET 页面。代码 sn-p 如下所示:

$('#sourcePageBtn').click( function() {
     $.post('destination.aspx', { name: "John", time: "2pm" }, function(data) {                    
     });
});

我正在尝试访问目标 aspx 文件的脚本部分中的参数,即

<script language="C#" runat="server">
    protected void Page_Load ( object src, EventArgs e) 
    {
     //Creating dynamic asp controls here
    }
</script>

我对脚本部分 Page_Load 中参数的具体需求源于我在 Page_Load 中创建了一些依赖于这些参数的动态图表控件。

问题: - 我在目标文件中看不到参数。我尝试使用Request["name"]Request["time"] 获取它们。

请告诉我哪里出了问题。

附注-我有这个SO post,它处理从源页面的jQuery部分启动一个新页面,最后除了这个参数捕获之外一切正常。

干杯

【问题讨论】:

    标签: asp.net post jquery


    【解决方案1】:

    不要将其作为发布请求发送,而是作为获取请求发送并尝试 Request.Params[0] 和 Request.Params[1]

    我刚刚创建了一个新的异步 httphandler 和 ajax 调用是这样的:

    $("#btnAjaxLoad").click(function() {
                    $.ajax({ type: "GET",
                    data: ({name : 'John', time : '8pm'}),                
                    url: "DataSourceAsync.ashx",  
                    contentType: "text/html; charset=utf-8",  
                    dataType: "html",  
                    success: function(data) { $("#AJAXGenerated").show(), $("#AJAXGenerated").html(data); $("#loading").hide(); }
                    });
            });
    

    现在我可以在 beginProcess 事件中访问 Jhon 和晚上 8 点作为 context.Request.Params[0] 和 context.Request.Params[1] 但只要我更改类型:GET 到 POST 两个参数都是不在那里。

    【讨论】:

      【解决方案2】:

      如果不查看所有代码很难说,但这可能是 2 个问题中的 1 个(或两者兼有)。

      首先它是什么样的按钮?提交按钮? asp.net 按钮(什么是提交按钮)或计划按钮?

      如果它们是提交按钮,那么这就是您的问题所在,因为它会执行完整的回发,并且您的 ajax 请求不会发生。

      第二种情况可能是在您的 jquery 代码中,您没有在加载时绑定 click 函数,因此该函数永远不会被绑定,因此当您单击按钮时它不会执行任何操作。

      代码

      <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
      
      <!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="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
      
      
          </script>
          <script type="text/javascript">
              $(function()
              {
                  $('#sourcePageBtn').click(function()
                  {
                      $.post('Default.aspx', { name: "John", time: "2pm" }, function(data)
                      {
                      });
                  });
              });
      
      
      </script>
      
      
      
      </head>
      <body>
          <form id="form1" runat="server">
          <div>
               <input id="sourcePageBtn" type="button" value="button" />
               <%--asp button won't work as it does full post back --%>
              <asp:Button ID="sourcePageBtn" runat="server" Text="Button" />
          </div>
      
          </form>
      </body>
      </html>
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      
      public partial class _Default : System.Web.UI.Page 
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              string test = Request["name"];
          }
      
      }
      

      如果你把它作为脚本标签也会有同样的结果。

      <script language="C#" runat="server">
          protected void Page_Load ( object src, EventArgs e) 
          {
           //Creating dynamic asp controls here
              string test = Request["name"];
          }
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-26
        • 1970-01-01
        相关资源
        最近更新 更多