jquery POST data in aspx page

2019-09-07 14:14发布

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!

2条回答
【Aperson】
2楼-- · 2019-09-07 14:37

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

查看更多
仙女界的扛把子
3楼-- · 2019-09-07 14:50

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); 
        }
    });
查看更多
登录 后发表回答