ajax不支持post,AJAX不能正确发送POST变量
你sendMessage功能是不完全正確 - 看看這看看它是否有幫助。
在最初檢查的receiveReq狀態(tài)不參照實例化XMLHttpRequest對象sendReq功能 - 也,請求絕不會因為open和send電話是代碼內(nèi)發(fā)送,即使它已經(jīng)使用sendReq檢查響應(yīng)的塊...
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"}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的ajax不支持post,AJAX不能正确发送POST变量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apk获取服务器时间文件名,如何根据AP
- 下一篇: 成都欢乐谷收费项目