ajax函数未定义
ajax function not defined
我对整个JavaScript和AJAX还很陌生。为了期末考试,我正在尝试创建一个网络应用程序。一个脚本将数据从用户发送到服务器,并将其保存到textfile
中,而另一个脚本始终向用户显示当前的textfile
。
我已经获得了在用户GUI上显示的文件的当前内容,我正在使用这个ajax
函数:
var xmlHttp = createXmlHttpRequestObject();
//erstellen des Objektes, welches nachher mit der anderen Seite kommuniziert
function createXmlHttpRequestObject(){
var xmlHttp;
if (window.ActiveXObject) {
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if (!xmlHttp) {
alert("cant create that object");
}
else
return xmlHttp;
}
//jede sekunde wird der inhalt von send_note geladen
setInterval(function note(){
if (xmlHttp.readyState==0 || xmlHttp.readyState==4) {
xmlHttp.open("POST", "send_note.php?", true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();
}
}, 500);
function handleServerResponse(){
if (xmlHttp.readyState==4) {
if (xmlHttp.status==200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("display").innerHTML = message;
setTimeout('note()', 1000);
}else{
alert('something went wrong');
}
}
}
CCD_ 4在加载用户所呈现的GUI的主体时被调用。
我现在无法工作的两件事:
- 如何使用我在这个ajax请求中使用的post变量将数据发送到响应的文件?我如何获取发送到该php文件中响应php文件的数据
- 谷歌chrome的开发工具向我显示了这个错误:Uncaught ReferenceError:note未定义
我调用note()
的段落如下:
<body onload="note()">
有人能帮我吗?
在setTimeout中定义函数note()。我觉得最好在外面定义一下。
当您的函数已经调用了note时,不要声明var note。
正如评论中所说,应该写入setTimeout setTimeout(注意,1000)
最后,既然使用interval,为什么要在同一个函数上使用setTimeout?这是为了改变间隔时间吗?这样不行。您只需在1秒后再次触发您的函数,而间隔将每500秒触发一次。
-
PHP必须回显任何输出以便ajax读取。通常是JSON响应,在JS中可以执行
function handleServerResponse(data) {JSON.decode(data);}
-
您正在从函数到字符串重写
note
。检查命名。
少量钞票
-
使用匿名函数:
setInterval(function () {});
-
您不需要
setTimeout('note()', 1000);
,因为您的代码将每500ms重复一次。 -
您是否考虑过使用jQuery ajax:
例如:
$.ajax({
url: 'send_note.php',
type: 'POST',
dataType: 'json',
data: {id: 2}, //js object with data to send to server.
success: function (response) {
alert(data); //data will contain anything that server outputs in "send_note.php"
}
});
相关文章:
- Jquery未定义函数正在停止其他操作
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- Uncaught ReferenceError:尝试在Android网络视图中访问时未定义函数
- jQuery文件上传和验证未定义函数
- 引用错误:未定义函数-Firefox
- res.render中出错,未定义函数
- 神秘的未定义函数
- 尝试使用函数和Ajax修改值时未定义函数
- 引用错误:未定义函数
- ReferenceError:未定义函数
- 在 Angular JS 中使用 ng-include 的未定义函数
- 未捕获的引用错误:未定义函数
- j查询工具提示引发错误:未定义函数
- 未捕获的引用错误: 未定义单击世界 -- 未定义函数
- JS:引用错误:未定义函数
- 茉莉花未定义函数
- Javascript 错误:未捕获的引用错误:未定义 [函数]
- 未定义函数
- 尽管 js 文件引用在标头中,但未定义函数
- Javascript 未定义函数错误