POST请求至.aspx文件返回空JSON问题求助
问题:POST JSON到.aspx后响应为空
我用JavaScript发送携带JSON请求体的POST请求到.aspx文件,需求是将JSON反序列化为Input对象,再序列化返回其中的obj字段,但在JavaScript中打印响应时,发现内容为空。
发送的请求体:
{"obj":"ASM10007-1A","objType":"part"}
预期响应:
{"obj":"ASM10007-1A"}
实际输出:控制台打印的Response对象无有效响应内容
.js 文件代码
async function JS_PostRequest(obj, objType) { var url = T_wroot + 'tweak/HS2-tweak/parts/excelcreator.exe.aspx'; console.log("URL: " + url); const data = { obj: obj, objType: objType, }; console.log("POST request body: " + JSON.stringify(data)); const options = { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }; await fetch(url, options) .then(function(res) { if (res.ok) { console.log("response:" + JSON.stringify(res)); return res; } if (!res.ok) { throw new Error(`HTTP error, status = ${res.status}`); } }) .catch((err) => { console.error(`Fetch error: ${err}`); }); }
.aspx 文件代码
<% string obj = Request.QueryString["o"]; string objType = Request.QueryString["t"]; try { var strJSON = String.Empty; //Request.InputStream.Position = 0; using (var inputStream = new StreamReader(Request.InputStream)) { strJSON = inputStream.ReadToEnd(); } var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Input>(strJSON); string message; if (data != null) // Check if 'data' is not null { if (data.obj != null) // Check if 'obj' inside 'data' is not null { message = data.obj; // Assign the value of 'data.obj' to 'message' } else { message = "No message received"; // Assign default value if 'obj' is null } } else { message = "No message received"; // Assign default value if 'data' is null } var responseObj = new { response = message }; string jsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(responseObj); Response.ContentType = "application/json"; Response.Write(jsonResponse); Response.End(); } catch (Exception e) { Response.Write("ASPX error: " + e.Message); } %> <script runat="server"> public class Input { public string obj { get; set; } public string objType { get; set; } } </script>
问题原因及修复方案
1. JavaScript端未读取响应体内容
当前代码仅打印了Response对象本身,没有解析并读取响应体的实际内容。需要调用res.json()方法获取JSON格式的响应数据:
修改JS的fetch回调逻辑:
await fetch(url, options) .then(function(res) { if (res.ok) { // 解析响应体为JSON对象 return res.json(); } throw new Error(`HTTP error, status = ${res.status}`); }) .then(function(responseData) { // 打印实际的响应数据 console.log("response data:", responseData); }) .catch((err) => { console.error(`Fetch error: ${err}`); });
2. ASPX端的两个关键问题
(1) 输入流位置未重置
ASP.NET可能已提前读取过请求输入流,导致StreamReader读取到空内容。取消注释Request.InputStream.Position = 0;,确保从流的起始位置读取:
Request.InputStream.Position = 0; using (var inputStream = new StreamReader(Request.InputStream)) { strJSON = inputStream.ReadToEnd(); }
(2) 响应结构与预期不符
当前ASPX返回的结构是{"response":"ASM10007-1A"},但需求是{"obj":"ASM10007-1A"},需修改响应对象的字段名:
var responseObj = new { obj = message };
修复后的ASPX核心代码片段
try { var strJSON = String.Empty; Request.InputStream.Position = 0; // 重置流位置 using (var inputStream = new StreamReader(Request.InputStream)) { strJSON = inputStream.ReadToEnd(); } var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Input>(strJSON); string objValue = "No message received"; if (data != null && data.obj != null) { objValue = data.obj; } var responseObj = new { obj = objValue // 匹配预期的响应结构 }; string jsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(responseObj); Response.ContentType = "application/json"; Response.Write(jsonResponse); Response.End(); } catch (Exception e) { Response.ContentType = "text/plain"; Response.Write("ASPX error: " + e.Message); Response.End(); }
内容的提问来源于stack exchange,提问作者Jonas Svare
相关产品推荐
相关产品推荐

