【发布时间】:2014-09-30 21:15:24
【问题描述】:
我正在尝试通过 Javascript 中的 AJAX 进行 HTTP POST(不使用 jquery)
ajax_post = function(aData) {
var k, v, xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", aData.path, true);
xmlhttp.setRequestHeader('Content-Type', 'application/json');
xmlhttp.send(JSON.stringify(aData));
return xmlhttp.responseText;
};
服务器端正在获取 POST 并正在发送响应。我知道一个响应正在到达客户端,因为我使用http://code.google.com/p/chrome-rest-client/ 测试了服务器,它响应了服务器按预期发送的 JSON。
我将整个 XMLHttpRequest 对象转储到控制台,但仍然看不到我缺少什么:
statusText
status 0
response
responseType
responseXML null
responseText
upload
XMLHttpRequestUpload {ontimeout: null, onprogress: null, onloadstart: null, onloadend: null, onload: null…}
withCredentials false
readyState 1
timeout 0
onreadystatechange null
ontimeout null
onprogress null
onloadstart null
onloadend null
onload null
onerror null
onabort null
open function open() { [native code] }
setRequestHeader function setRequestHeader() { [native code] }
send function send() { [native code] }
abort function abort() { [native code] }
getAllResponseHeaders function getAllResponseHeaders() { [native code] }
getResponseHeader function getResponseHeader() { [native code] }
overrideMimeType function overrideMimeType() { [native code] }
UNSENT 0
OPENED 1
HEADERS_RECEIVED 2
LOADING 3
DONE 4
addEventListener function addEventListener() { [native code] }
removeEventListener function removeEventListener() { [native code] }
dispatchEvent function dispatchEvent() { [native code] }
我的客户端 POST 请求有什么问题?
【问题讨论】:
-
您可以尝试使用 jQuery ajax。使用 jQuery ajax,您不必担心 xmlHttp 对象。
-
浏览器开发工具 (F12) 可以帮助跟踪网络请求,并确定响应中是否真的有文本。没有实际内容的 200-OK 响应仍然有效。
-
状态0和RS1应该是第一条线索,但是你需要将true改为false或者使用asyn等待responseText
标签: javascript ajax post