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 ?

3 Answers 3

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);
      }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

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.
I'll admit it - I forgot :) I don't use ASP.NET much. Sorry.
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"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.