代码之家  ›  专栏  ›  技术社区  ›  Arnkrishn

ASP没有看到Ajax参数。网

  •  0
  • Arnkrishn  · 技术社区  · 16 年前

    我正在从源ASP传递Ajax参数。NET页面到目标ASP。网页。代码片段如下所示:

    $('#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部分启动一个新页面的问题,最后除了这个参数捕获之外,所有的工作都很好。

    干杯

    2 回复  |  直到 9 年前
        1
  •  1
  •   A G    16 年前

    而不是作为post请求发送,而是作为get请求和try请求发送。参数[0]和请求。参数[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和8pm。要求参数[0]和上下文。要求参数[1]但一旦我更改类型:GET to POST,两个参数都不存在。

        2
  •  1
  •   chobo2    16 年前

    很难说没有看到所有代码,但这可能是两个问题中的一个(或两个都有)。

    首先是什么样的按钮?提交按钮?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>