Ajax帖子显示的消息不足 [英] Ajax post displaying an inadequate message
本文介绍了Ajax帖子显示的消息不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是AJAX方法的新手.我想发布一些由php页面处理的信息,将其称为page.php
I am new to AJAX methods. I want to post some infos that are processes by a php page, call it page.php
在我的html页面中,我输入了以下代码:
In my html page, I have put this code :
<script type="text/javascript" charset="utf-8">
//I have put the function getXMLHttpRequest() on a separate js file
function getXMLHttpRequest() {
var xhr = null;
if (window.XMLHttpRequest || window.ActiveXObject) {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhr = new XMLHttpRequest();
}
} else {
alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
return null;
}
return xhr;
}
function request(callback) {
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
callback(xhr.responseText);
}
};
xhr.open("POST", "page.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("name=proposition");
}
function postData(Data) {
alert(Data);
}
</script>
<button onclick="request(postData);">...</button>
在page.php中,我有一个方法
In page.php, I have a method
protected function comment(){
//some code processing the posted infos (that works fine)...
//to debug, I have put a
echo('Hello world');
}
事实是,我没有收到任何"Hello world"消息,而是一条巨大的警报消息,其中显示了我所有的网页代码.
The fact is I don't get any 'Hello world' but a huge alert message with all my webpage code displayed.
有人有想法吗?
最好, 纽本
推荐答案
您正在page.php中定义一个函数,但永远不要调用它. 并删除受保护的关键字,因为它没有意义(您不是在上课)
You are defining a function in page.php, but never calling it. And remove the protected keyword because it doesn't make sense (you are not making a class)
尝试
function comment(){
//some code processing the posted infos (that works fine)...
//to debug, I have put a
echo('Hello world');
}
comment();
这篇关于Ajax帖子显示的消息不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文