XMLHTTP请求发布数据挂起

时间:2015-08-30 11:50:36

标签: javascript ajax

我正在尝试使用post方法创建一个AJAX调用,并且无法让它正常工作。该脚本在处理阶段挂起(readyState不会继续执行4)。 如果有人能在这里解决这个问题,我会很感激。我看了几个教程,似乎我的代码 - 应该工作。

function newRequestPost(url, post, threadid, cfunc) {
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var params = "post="+post+"&threadid="+threadid;
	xmlhttp.onreadystatechange = cfunc;
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(params);
}

function openModalPreview(threadid) {
	var post = document.getElementById("post_txt").value;
	newRequestPost("url", post, threadid, function() {
		if(xmlhttp.readyState == 1) {
			document.getElementById("that_which_lies_in_the_modal").innerHTML = "Loading...";
		}
		if(xmlhttp.readyState == 2) {
			document.getElementById("that_which_lies_in_the_modal").innerHTML = "Received";
		}
		if(xmlhttp.readyState == 3) {
			document.getElementById("that_which_lies_in_the_modal").innerHTML = "Processing...";
		}
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("that_which_lies_in_the_modal").innerHTML = xmlhttp.responseText;
		}
	});
}

0 个答案:

没有答案