2

Does anyone know what is it going on here? I have try to pass a value from ajax to .aspx, but somehow the value seem doesn't pass over successfully.

Following is my code:

  $.ajax({
      type: "POST",
      url: "pgtest.aspx",
      data: "sState=VIC",
      success: function (msg) {
          alert("Data Saved: " + msg);
      }
  });

and this is my code inside my .net c#:

newTest.Value = Request.QueryString["sState"];

Somehow the for Request.QueryString["sState"] is empty in .net c#. Does anyone know what is going wrong here ?

Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
Jin Yong
  • 42,698
  • 72
  • 141
  • 187

3 Answers3

1

When passing data in POST, the data is not passed in Request.QueryString, it's passed into Request.Form instead. Try

newTest.Value = Request.Form["sState"];

Another thing I'd change is the jQuery call - use a data object instead of just a string, a such:

$.ajax({
      type: "POST",
      url: "pgtest.aspx",
      data: { sState: "VIC" },
      success: function (msg) {
          alert("Data Saved: " + msg);
      }
});
configurator
  • 40,828
  • 14
  • 81
  • 115
0

You need to use GET request as it is light in nature but less secured too and it is passed in querystring.:

$.ajax({
      type: "GET",
      url: "pgtest.aspx?sState=VIC",      
      success: function (msg) {
          alert("Data Saved: " + msg);
      }
  });

Now you will get below values:

newTest.Value = Request.QueryString["sState"];
0

Request.QueryString is for GET requests only. For POST requests, you need Request.Form. See also: Get POST data in C#/ASP.NET

Community
  • 1
  • 1
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • When you use the `Request` indexer (`Request[...]`), you can get values from either `QueryString`, `Form`, `Cookies` or `ServerVariables`. If you know which one the value should be in - and you should always be able to know - use that. In this case, like I mentioned in my answer, it's `Form`. – configurator May 12 '11 at 02:53
  • I'll admit it - I forgot :) I don't use ASP.NET much. Sorry. – Ry- May 12 '11 at 02:57