jquery POST data in aspx page

送分小仙女□ 提交于 2020-03-26 02:46:05

问题


I post data to my aspx file qith the following code:

 $.ajax({
            type: 'POST',
            url: "Ajax_Text.aspx?rand=" + myRand
                                        + "&id=" + $(".articleID").attr('title')
                                        + "&text=" + $("#text").val(),
            cache: false,
            beforeSend: function () {

            },
            success: function (data) {
                alert(data); 
            }
        });

Why i catch the text value by using the following code

 string text = "";

            if (!String.IsNullOrEmpty(Request.QueryString["text"]))
            {
                text = Request.QueryString["text"].ToString();
            }
            else
            {
                text = "";
            }

and not this code:

string text = "";

            if (!String.IsNullOrEmpty(Request.Form["text"]))
            {
                text = Request.Form["text"].ToString();
            }
            else
            {
                text = "";
            }

Why is that? I expected Request.Form to work as i post data with jquery! Any ideas?

I suspect that the problem is that i have my input in the url parameter. Maybe i should put it to a data parameter but that means it will become a json request!


回答1:


POST data are not send in query string but added to the request body. Try this code:

$.ajax({
        type: 'POST',
        url: "Ajax_Text.aspx",
        data: {'rand': myRand, 'id': $(".articleID").attr('title'), 'text': $("#text").val()},
        cache: false,
        beforeSend: function () {

        },
        success: function (data) {
            alert(data); 
        }
    });



回答2:


You are "posting" the data (text) as a query string (as part of URL) so you have to use Request.QueryString.



来源:https://stackoverflow.com/questions/12656833/jquery-post-data-in-aspx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!