如何通过 ASP.Net context.Request 检索 JSON
var OrderInfo = {"ProductID":
"ProductIDValue",
"ProductName": "ProductName",
"Quantity": 1,
"Amount": 9999,
"SLQuantity": 9999,
"SLDate": "08/03/2010"
};
var DTO = { 'OrderInfo': OrderInfo };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "JasonHandler.ashx",
data: JSON.stringify(DTO),
dataType: "json"
});
我试图通过此代码在 ASHX 文件中的服务器端检索发布的 JSON 数据:
string strrequest = context.Request["OrderInfo"];
但它总是返回 null。我做错了什么?
var OrderInfo = {"ProductID":
"ProductIDValue",
"ProductName": "ProductName",
"Quantity": 1,
"Amount": 9999,
"SLQuantity": 9999,
"SLDate": "08/03/2010"
};
var DTO = { 'OrderInfo': OrderInfo };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "JasonHandler.ashx",
data: JSON.stringify(DTO),
dataType: "json"
});
I'm trying to retrieve posted JSON data on server side in an ASHX file via this code:
string strrequest = context.Request["OrderInfo"];
but it always return null. What Am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 http://dailydotnettips.com/2013/ 09/26/从jquery发送原始json请求到asp-net/
From http://dailydotnettips.com/2013/09/26/sending-raw-json-request-to-asp-net-from-jquery/
挖掘互联网。我发现 IE 在接收完整的 POST 请求时出现问题。 @ronaldwidha 对 InputStream 的建议与我发现的类似。但我没有使用 javascriptserializer,而是使用 JSON.NET 代码片段如下,我希望这能帮助其他遇到类似问题的人
Digging the Internet. I found out that IE has problem receiving POST request in full. @ronaldwidha's suggestion on InputStream is similar to what I have found. But rather than using javascriptserializer I use JSON.NET Code snippets is below and I hope this would help other with similar problem
Request[] 只会查看表单参数和 quetystring。您将需要执行表单发布或使用 qs 或自己解析请求正文。
Request[] will only look at form params and quetystring. You will need to do a form post or use qs or parse the request body yourself.
我认为您可以从 HttpCurrent.Context.Request.GetResponse() 中获取请求正文。
首先验证内容类型标头可能是个好主意。
I think you could get the request body out of HttpCurrent.Context.Request.GetResponse().
Its probably a good idea to verify the content-type header first.