日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ajax不支持post,AJAX不能正确发送POST变量

發布時間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ajax不支持post,AJAX不能正确发送POST变量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

你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 tests

var 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"}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的ajax不支持post,AJAX不能正确发送POST变量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。