我目前有一些 jquery 将数据发布到我的网页之一上。
现在我只是想让它发布一些 JSON 来测试它,但我不知道发布后是否必须在后端实际获取数据。
我一直使用 Request.Params
来获取发布的数据,但这次似乎不起作用。
这是我用来发帖的代码:
// 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
});
不过,该帖子绝对是成功的,因为我可以使用 Fiddler 看到它,而且当我检查 Request.ContentLength
时,它会返回已发布的正确字节数。
但我在任何地方都找不到实际数据。 关于我做错了什么有什么想法吗?
提前致谢。
最佳答案
发布 javascript 对象:
- 将普通对象传递给数据选项,
- 保留 contentType 选项。默认选项是完美的。
然后您就可以访问 Request 集合中对象的属性值,就像您发布了表单一样。
服务器端:
string input;
using(var reader = new StreamReader(Request.InputStream)){
input = reader.ReadToEnd();
}
发布 Json:
- 数据:JSON.stringify(person),
- 内容类型:“application/json”
服务器端:
string json;
using(var reader = new StreamReader(Request.InputStream)){
json = reader.ReadToEnd();
}
var person = Json.Decode(json);
引用自: http://www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages
关于jquery - 如何在 ASP.NET Web 表单中获取 JSON POST 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29175594/