Jin Yong
Jin Yong

Reputation: 43778

unable to pass post value over from ajax to the page in .net c#

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 ?

Upvotes: 2

Views: 1571

Answers (3)

user12187129
user12187129

Reputation:

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"];

Upvotes: 0

Ry-
Ry-

Reputation: 224904

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

Upvotes: 0

configurator
configurator

Reputation: 41630

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);
      }
});

Upvotes: 1

Related Questions