jquery POST data in aspx page
Asked Answered
G

2

0

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!

Gearhart answered 29/9, 2012 at 21:54 Comment(0)
C
2

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); 
        }
    });
Czardom answered 29/9, 2012 at 22:9 Comment(2)
Yes, that came up to my mind after that but now the c# block of code missed the input! Firebug: Source { "id": "1", "text": "test222" }Gearhart
Sorry my fault! I experimented with my code but I put all the data parameter in a string! I looked to your code and i found that i was wrong with my attempt! At least i managed to figure out what i was doing wrong! Thanks a lot!Gearhart
G
1

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

Goodfellowship answered 29/9, 2012 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.