你sendMessage功能是不完全正确 - 看看这看看它是否有帮助。
在最初检查的receiveReq状态不参照实例化XMLHttpRequest对象sendReq功能 - 也,请求绝不会因为open和send电话是代码内发送,即使它已经使用sendReq检查响应的块...
var sendReq = getXmlHttpRequestObject();
function messageSent(response){
console.info(response);
}
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
document.getElementById('status').innerHTML = 'Status: Error while creating XmlHttpRequest Object.';
}
}
/*
Set the `param` as a parameter to the function, can reuse it more easily.
*/
function sendMessage(param) {
if(sendReq){
/* set the listener now for the response */
sendReq.onreadystatechange=function(){
/* Check for the request Object's status */
if(sendReq.readyState==4) {
if(sendReq.status==200){
/* Process response here */
messageSent.call(this, sendReq.response);
} else {
/* there was an error */
}
}
};
/* Open & send request, outwith the listener */
sendReq.open("POST", 'chatServer.php', true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.send(param);
document.getElementById("message").value = "";
}
}
/* send some messages */
sendMessage('message=ciao');
sendMessage('message=ajax...sent via POST');
最初错过了param var声明,所以纠正了这个错误。
更新
chatserver.php (example)
------------------------
/*
demo_chatserver.php
*/
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
/*
include your db connection
set your headers
*/
if(isset($_POST['message']) && !empty($_POST['message'])){
@ob_clean();
/* Create the db conn && test that it is OK */
/* for the purposes of the tests only */
$_POST['date']=date(DATE_COOKIE);
echo json_encode($_POST, JSON_FORCE_OBJECT);
exit();
}
}
?>
html/php page
---------------
ajax testsvar sendReq = getXmlHttpRequestObject();
function messageSent(response){
console.info('This is the response from your PHP script: %s',response);
if(document.getElementById("message")) document.getElementById("message").innerHTML=response;
}
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
document.getElementById('status').innerHTML = 'Status: Error while creating XmlHttpRequest Object.';
}
}
/*
Set the `param` as a parameter to the function, can reuse it more easily.
*/
function sendMessage(param) {
if(sendReq){
/* set the listener now for the response */
sendReq.onreadystatechange=function(){
/* Check for the request Object's status */
if(sendReq.readyState==4) {
if(sendReq.status==200){
/* Process response here */
messageSent.call(this, sendReq.response);
} else {
/* there was an error */
}
}
};
/* Open & send request, outwith the listener */
/*NOTE: I have this in a folder called `test`, hence the path below!! */
sendReq.open("POST", '/test/demo_chatserver.php', true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.send(param);
if(document.getElementById("message")) document.getElementById("message").innerHTML = "";
}
}
/* send some data - including original 'message=ciao' but other junk too */
window.οnlοad=function(event){
sendMessage('message=ciao&id=23&banana=yellow&php=fun&method=post&evt='+event);
}
Should output something like:-
------------------------------
{"message":"ciao","id":"23","banana":"yellow","php":"fun","method":"post","evt":"[object Event]","time":1446730182,"date":"Thursday, 05-Nov-15 13:29:42 GMT"}