简体   繁体   English

访问Request.QueryString [foo]为空,但URL显示查询字符串/参数就在那

[英]Accessing Request.QueryString[foo] is null but the URL shows the query string/parameter is right there

I am calling an .aspx script through AJAX. 我正在通过AJAX调用.aspx脚本。 In that script I am attempting to get a value from the query string using Request.QueryString["i"] but it always returns null even though, if I examine the Request object in debug mode, the query string IS right there. 在该脚本我试图让使用从查询字符串的值Request.QueryString["i"]但它始终返回null即使,如果我检查调试模式Request对象,查询字符串就在那里。

Whats going wrong & how can I retrieve the i parameter value from testScript.aspx?i=199? 怎么了?如何从testScript.aspx中检索i参数值?i = 199?

Heres my simple code: 这是我的简单代码:

    public partial class getData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            short index = System.Convert.ToInt16(Request.QueryString["i"]);  // BREAKPOINT
        }
    }

When I use a break point & examine the Request Object I can see that the Request.QueryString variable is empty(just a {}). 当我使用断点并检查Request对象时,我可以看到Request.QueryString变量为空(只是{})。 Request.QueryString["i"] is null. Request.QueryString["i"]为空。

If you look at the following img you can see the form has my i parameter(thats my query string .aspx?i=4 如果查看以下img,您会看到该表单具有我的i参数(即我的查询字符串.aspx?i=4

在此处输入图片说明

Your form is sent using POST request, and parameter i is not in QueryString but in request body encoded using multipart form data format, Request.QueryString only show's parameters passed through URI like page.asax?i=4. 您的表单是使用POST请求发送的,而参数i不在QueryString中,而是在使用多部分表单数据格式编码的请求正文中, Request.QueryString仅显示通过URI传递的参数,例如page.asax?i = 4。 Use Request.Form["i"] 使用Request.Form["i"]

I think you might be confusing the query string with form fields. 我认为您可能会将查询字符串与表单字段混淆。 In the screenshot, your value "i" is clearly on the Form property. 在屏幕截图中,您的值“ i”显然位于Form属性上。

Form fields are fields on the page which are POSTed, as opposed to the query string items, which appear at the end of the url. 表单字段是页面上已过帐的字段,与查询字符串项目相对,后者显示在url的末尾。

If you look at the QueryString property, I suspect you won't find your item. 如果您查看QueryString属性,可能会找不到您的项目。

Try using: 尝试使用:

Request.Form["i"]

Hello Jake u are used post method, and in post method query string not worked, so u have to user either get method to use query string or if u are used post method then change your code in testScript.aspx load event Request.QueryString to Request.Form like this 您好杰克,您使用post方法,而在post方法中查询字符串不起作用,因此您必须使用get方法来使用查询字符串,或者如果您使用了post方法,则将testScript.aspx加载事件Request.QueryString中的代码更改为这样的Request.Form

public partial class getData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        short index = System.Convert.ToInt16(Request.Form["i"]);  // BREAKPOINT
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM