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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值