object 存储函数的问题
Problems with object stored functions
所以,我创建了一个函数,它创建一个新的 xmlhttprequest 并接受一个 object,其中包含构造一个请求所需的所有信息作为参数。一切正常,直到我通过 object 告诉它完成请求后该怎么做(该函数的默认值完美运行。),出于某种原因,它给了我一个错误 "Uncaught ReferenceError: x is not defined"
window._={
xhr: function(a){
if(typeof a.method==="undefined"||a.adress==="undefined"){
console.log("Well, That's not going to work...");
}
var x=new XMLHttpRequest();
x.open(a.method,a.adress,a.asyn);
if(typeof a.headers!== "undefined"){
Object.keys(a.headers).forEach(function (key) {
x.setRequestHeader(key,a.headers[key]);
});
}
if(typeof a.ready==="undefined"){
a.ready=function(){console.log(x.responseText);}
}
x.send();
x.onreadystatechange = function(){
if(x.readyState==4&&x.status==200){
a.ready();
}
}
}
}
window.onload = alert(_.xhr({method:"get",adress:"../php/include/functions.php?function=id_from_username&arg=kiddo",asyn:true,headers:{},ready:function(){console.log(x.responseText);}}));
(指定的 url 是我服务器上的一个 php 文件,它可以在给定 GET headers ?function=samplefunc&arg1=foo&arg2=bar
时执行文件内的功能,并且可以选择尽可能多的参数功能需要。在这种情况下,我告诉它寻找名称为 "kiddo" 且其 ID 为 return 的用户到我的脚本。)
问题是您正在从其范围之外访问 xhr 函数内部定义的变量 x。
解决方案是使用 xhr 函数内部的响应文本调用 ready 函数
window._={
xhr: function(a){
if(typeof a.method==="undefined"||a.adress==="undefined"){
console.log("Well, That's not going to work...");
}
var x=new XMLHttpRequest();
x.open(a.method,a.adress,a.asyn);
if(typeof a.headers!== "undefined"){
Object.keys(a.headers).forEach(function (key) {
x.setRequestHeader(key,a.headers[key]);
});
}
if(typeof a.ready==="undefined"){
a.ready=function(){console.log(x.responseText);}
}
x.send();
x.onreadystatechange = function(){
if(x.readyState==4&&x.status==200){
a.ready(x.responseText);
}
}
}
}
window.onload = alert(_.xhr({method:"get",adress:"",asyn:true,headers:{},ready:function(responsText){console.log(responsText);}}));
所以,我创建了一个函数,它创建一个新的 xmlhttprequest 并接受一个 object,其中包含构造一个请求所需的所有信息作为参数。一切正常,直到我通过 object 告诉它完成请求后该怎么做(该函数的默认值完美运行。),出于某种原因,它给了我一个错误 "Uncaught ReferenceError: x is not defined"
window._={
xhr: function(a){
if(typeof a.method==="undefined"||a.adress==="undefined"){
console.log("Well, That's not going to work...");
}
var x=new XMLHttpRequest();
x.open(a.method,a.adress,a.asyn);
if(typeof a.headers!== "undefined"){
Object.keys(a.headers).forEach(function (key) {
x.setRequestHeader(key,a.headers[key]);
});
}
if(typeof a.ready==="undefined"){
a.ready=function(){console.log(x.responseText);}
}
x.send();
x.onreadystatechange = function(){
if(x.readyState==4&&x.status==200){
a.ready();
}
}
}
}
window.onload = alert(_.xhr({method:"get",adress:"../php/include/functions.php?function=id_from_username&arg=kiddo",asyn:true,headers:{},ready:function(){console.log(x.responseText);}}));
(指定的 url 是我服务器上的一个 php 文件,它可以在给定 GET headers ?function=samplefunc&arg1=foo&arg2=bar
时执行文件内的功能,并且可以选择尽可能多的参数功能需要。在这种情况下,我告诉它寻找名称为 "kiddo" 且其 ID 为 return 的用户到我的脚本。)
问题是您正在从其范围之外访问 xhr 函数内部定义的变量 x。
解决方案是使用 xhr 函数内部的响应文本调用 ready 函数
window._={
xhr: function(a){
if(typeof a.method==="undefined"||a.adress==="undefined"){
console.log("Well, That's not going to work...");
}
var x=new XMLHttpRequest();
x.open(a.method,a.adress,a.asyn);
if(typeof a.headers!== "undefined"){
Object.keys(a.headers).forEach(function (key) {
x.setRequestHeader(key,a.headers[key]);
});
}
if(typeof a.ready==="undefined"){
a.ready=function(){console.log(x.responseText);}
}
x.send();
x.onreadystatechange = function(){
if(x.readyState==4&&x.status==200){
a.ready(x.responseText);
}
}
}
}
window.onload = alert(_.xhr({method:"get",adress:"",asyn:true,headers:{},ready:function(responsText){console.log(responsText);}}));