无法将 post 值从 ajax 传递到 .net c# 中的页面

发布于 2024-11-06 10:18:40 字数 444 浏览 1 评论 0原文

有谁知道这是怎么回事?我尝试将值从 ajax 传递到 .aspx,但不知何故,该值似乎没有成功传递。

以下是我的代码:

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

这是我的 .net c# 中的代码:

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

不知何故, for Request.QueryString["sState"] 在 .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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2024-11-13 10:18:41

在 POST 中传递数据时,数据不是在 Request.QueryString 中传递,而是传递到 Request.Form 中。尝试

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

我要更改的另一件事是 jQuery 调用 - 使用数据对象而不仅仅是字符串,例如:

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

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);
      }
});
笑忘罢 2024-11-13 10:18:41

Request.QueryString 仅适用于 GET 请求。对于 POST 请求,您需要 Request.Form。另请参阅:在 C#/ASP.NET 中获取 POST 数据

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

反目相谮 2024-11-13 10:18:41

您需要使用 GET 请求,因为它本质上很轻,但安全性也较低,并且它是在查询字符串中传递的。:

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

现在您将获得以下值:

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

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"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文