jquery POST data in aspx page

2019-09-07 13:55发布

问题:

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.