我有以下对象:
var payload = {
someProperty: null
};
我使用HTTP将对象发送到API端点。
$.ajax({
type: "POST",
url: nodeHost + ":" + agentWebPort + "/assignDisposition/",
data: payload,
beforeSend: function(request) {
request.setRequestHeader("session", cookieSessionID);
request.setRequestHeader("user", cookieUsername);
},
success: function(response) {;
} //end success
});
在(Node.js)服务器上,我按如下方式查看来自HTTP请求的数据:
console.log(payload);
令我惊讶的是,这是输出:
{someProperty:''}
即使我的属性值源自NULL,但一旦到达服务器,它也是一个空字符串。
这是正常行为吗?
答案 0 :(得分:1)
我尝试通过asp.net读取请求的正文
ajaxText.apsx
string req_txt;
using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
req_txt = reader.ReadToEnd();
}
Response.Write(req_txt); var xmlhttp = new XMLHttpRequest();
javascript
var payload = {
someProperty: null
};
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4
if (xmlhttp.status == 200) {
console.log("XMLHttpRequest : " + xmlhttp.responseText);
}
else if (xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("POST", "/Test/ajaxTest.aspx", true);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(payload));
XMLHttpRequest的请求为{“ someProperty”:null}
$.ajax({
type: "POST",
url: "/Test/ajaxTest.aspx",
data: payload,
success: function (response) {;
console.log("$.ajax : " + response);
}
});
jquery ajax的结果为someProperty:''
您可以看到结果的差异。
jquery会将json对象序列化为字符串,并在此过程中从null更改为“”。