AJAX,将附加变量传递给回调并将 XMLHTTLRequest.response 存储到变量
AJAX, pass additional variable to callback and store XMLHTTLRequest.response to variable
我正在尝试使用标准函数 loadDoc(url, cfunc) 读取服务器上的本地文件,然后
1) 在文件中搜索特定字符串 (getLine());
2) 如果可能,将该行存储到一个变量中。
对于第 1 点,我将一个字符串传递给回调。
2) 获取响应是有问题的,因为 XMLHTTPRequest 是异步的。此时错误是:
"ReferenceError: xhttp is not defined"
function main(){
var url="data.txt"
var str="1,0,"; //just an example
var myCallBackWithVar = function(){
getLine(str);
};
loadDoc(url, myCallBackWithVar);
//Can I get the line here somehow?
}
function loadDoc(url, cfunc) {
var xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
cfunc(xhttp);
}
}
xhttp.overrideMimeType('text/plain');
xhttp.open("GET", url, true);
xhttp.send();
}
//Find string with the desired data in txt file
function getLine(str) {
var data=xhttp.responseText;
//Find the line from the txt file
var start=data.indexOf(str);
var end=data.indexOf(";",start);
var line=data.substring(start,end);
return line;
}
data.txt 是这样的:
some data here
0,0,9;
1,0,10;
1,1,11;
我已经尝试传递 XMLHTTPRequest objetct getLine(xhttp,str)。如何解决第1点和第2点?我宁愿暂时免费使用它 jQuery。谢谢
完成后,您不需要通过回调函数传递整个 xhttp 变量。当您这样做时:
function getLine(str) {
var data=xhttp.responseText;
xhttp 已经超出范围。要解决此问题,参数名称也必须是 xhttp。
更好的方法是:
cfunc(xhttp.responseText);
然后
var data=str
这样,您只传递需要的参数。
Can I get the line here somehow?
我认为这不是个好主意。您无法确定您的应用程序是否会正常运行。 XHR 是一个异步函数,你应该使用异步架构。
这里是如何完成此功能的示例。
var text; // define global variable
var str = "1,0,"; //just an example
function main(){
var url = "data.txt";
var cb = function (data){
text = getLine(data);
// you can use text var here
// or in anyewhere in your code
}
loadDoc(url, cb);
}
function loadDoc(url, cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
cb(xhr.responseText);
}
}
xhr.overrideMimeType('text/plain');
xhr.open("GET", url, true);
xhr.send();
}
//Find string with the desired data in txt file
function getLine(data) {
if(data) {
//Find the line from the txt file
var start = data.indexOf(str);
var end = data.indexOf(";", start);
var line = data.substring(start, end);
return line;
}
}
我正在尝试使用标准函数 loadDoc(url, cfunc) 读取服务器上的本地文件,然后
1) 在文件中搜索特定字符串 (getLine());
2) 如果可能,将该行存储到一个变量中。
对于第 1 点,我将一个字符串传递给回调。 2) 获取响应是有问题的,因为 XMLHTTPRequest 是异步的。此时错误是: "ReferenceError: xhttp is not defined"
function main(){
var url="data.txt"
var str="1,0,"; //just an example
var myCallBackWithVar = function(){
getLine(str);
};
loadDoc(url, myCallBackWithVar);
//Can I get the line here somehow?
}
function loadDoc(url, cfunc) {
var xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
cfunc(xhttp);
}
}
xhttp.overrideMimeType('text/plain');
xhttp.open("GET", url, true);
xhttp.send();
}
//Find string with the desired data in txt file
function getLine(str) {
var data=xhttp.responseText;
//Find the line from the txt file
var start=data.indexOf(str);
var end=data.indexOf(";",start);
var line=data.substring(start,end);
return line;
}
data.txt 是这样的:
some data here
0,0,9;
1,0,10;
1,1,11;
我已经尝试传递 XMLHTTPRequest objetct getLine(xhttp,str)。如何解决第1点和第2点?我宁愿暂时免费使用它 jQuery。谢谢
完成后,您不需要通过回调函数传递整个 xhttp 变量。当您这样做时:
function getLine(str) {
var data=xhttp.responseText;
xhttp 已经超出范围。要解决此问题,参数名称也必须是 xhttp。
更好的方法是:
cfunc(xhttp.responseText);
然后
var data=str
这样,您只传递需要的参数。
Can I get the line here somehow?
我认为这不是个好主意。您无法确定您的应用程序是否会正常运行。 XHR 是一个异步函数,你应该使用异步架构。
这里是如何完成此功能的示例。
var text; // define global variable
var str = "1,0,"; //just an example
function main(){
var url = "data.txt";
var cb = function (data){
text = getLine(data);
// you can use text var here
// or in anyewhere in your code
}
loadDoc(url, cb);
}
function loadDoc(url, cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
cb(xhr.responseText);
}
}
xhr.overrideMimeType('text/plain');
xhr.open("GET", url, true);
xhr.send();
}
//Find string with the desired data in txt file
function getLine(data) {
if(data) {
//Find the line from the txt file
var start = data.indexOf(str);
var end = data.indexOf(";", start);
var line = data.substring(start, end);
return line;
}
}