简体   繁体   English

如何在ASP.NET Web窗体中获取JSON POST数据?

[英]How to get JSON POST data in ASP.NET Web Forms?

I currently have some jquery that is POSTing data onto one of my web pages. 我目前有一些jquery将数据发布到我的一个网页上。

Right now I'm just trying to get it to post some JSON to test it out, but I can't figure out have to actually get the data in my back-end once it's posted. 现在我只是试图让它发布一些JSON来测试它,但我不知道在发布之后我必须真正获得后端的数据。

I've always used Request.Params to get posted data, but it doesn't seem to be working this time. 我总是使用Request.Params来获取发布的数据,但这次似乎没有工作。

This is the code I'm using to do the post: 这是我用来做帖子的代码:

// This data is just for testing purposes, doesn't actually do anything
var person = {
    name: "Bob",
    address: "123 Main St.",
    phone: "555-5555"
}

var jqxhr = $.ajax({
    type: "POST",
    url: "/example/mypage.aspx",
    contentType: 'application/json; charset=utf-8',
    dataType: "json",
    timeout: 0,
    success: function () {
        alert("Success");
    },
    error: function (xhr, status, error) {
        alert(error);
    },
    data: person
});

The post is definitely successful though, as I can see it using Fiddler, plus when I check Request.ContentLength it returns the right number of bytes that was posted. 这个帖子肯定是成功的,因为我可以看到它使用Fiddler,另外当我检查Request.ContentLength它返回发布的正确字节数。

But I can't find the actual data anywhere. 但我无法在任何地方找到实际数据。 Any ideas on what I'm doing wrong? 关于我做错了什么的任何想法?

Thanks in advance. 提前致谢。

Posting javascript object: 发布javascript对象:

  1. pass the plain object to the data option, 将普通对象传递给数据选项,
  2. leave the contentType option alone. 单独保留contentType选项。 The default option is perfect. 默认选项是完美的。

Then you can access the property values of the object in the Request collection as if you have posted a form. 然后,您可以访问Request集合中对象的属性值,就像您已发布表单一样。

server side: 服务器端:

   string input;
    using(var reader = new StreamReader(Request.InputStream)){
            input = reader.ReadToEnd();
        }

Posting Json: 张贴Json:

  1. data: JSON.stringify(person), data:JSON.stringify(person),
  2. contentType: "application/json" contentType:“application / json”

server side: 服务器端:

string json;
using(var reader = new StreamReader(Request.InputStream)){
        json = reader.ReadToEnd();
    }
var person = Json.Decode(json);

Referenced from: http://www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages 参考自: http//www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM