Javascript GET回调函数未在服务器响应中定义
Javascript GET callback function not defined on server response
我已经创建了一个包含与我的问题相关的所有内容的版本:
//new version doesn't work ->
function global_function(server_response){}
function Constructor(global_function){
this.local_reference_to_global_function=global_function;}
Constructor.prototype.api_function(){
//this.localreference_to_global_function("test"); // works
//global_function("test"); // works
xmlhttp=XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
this.local_reference_to_global_function(xmlhttp.responseText)}
// does not work on asynchronuous callback
// probably scope/visibility/closure lifetime/whatever is wrong
// and I need to understand what is wrong to fix it in my code .
// firebug said that "this.local_reference_to_global_function is not a function"
// the first 2 calls in this function work
//so I am puzzled that this doesn't
xmlhttp.open("GET",...);
xmlhttp.send();}
var derivativeConstructor;
function server_query(){
if(derivativeConstrucror===undefined){
var derivativeConstructor=new Constructor(global_function);}
derivativeConstructor.api_function();}
//old version works but clutters my code so I don't want it ->
//the actual code saves more lines of code
//and has better visibility
// so it's worth it
//unlike this example
function server_query(){xmlhttp=XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
global_function(xmlhttp.responseText)}
xmlhttp.open("GET",...);
xmlhttp.send();} // this worked and all the rest should be practically unchanged
// where it all get's called from - should be unchanged between versions ->
<form action="" method="GET" onsubmit="(function(){return false;});">
<input type="button" value="TEST" onclick="server_query();">
</form>
当我说我想改变的旧版本工作时,我是认真的。我的问题是,我没有第一次发布我的全部代码,所以人们会觉得我一开始就没有尝试,然后就没有进一步研究我真正问的是什么。现在我也试着简化一些东西,以消除我认为问题所在的地方的混乱,所以这不是实际的代码,可能无法在浏览器中复制粘贴-可能存在"小问题"。我只对我问的问题感兴趣:我在函数可见性/闭包变量可见性/变量生命周期/任何地方出错了,我该如何修复它。
旧版本的问题:我被两个回答/评论的人贴上了不定义自己功能的标签->
我有这个javascript代码,应该简化创建一个服务器GET:
function Generic_server_request(
server_location,
server_file,
client_callback_function){
this.server_location=server_location;
this.server_file=server_file;
this.query_parameters="";
this.client_callback_function=client_callback_function;}
Generic_server_request.prototype
.server_request=function(){
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState===4
&& xmlhttp.status===200)
this.client_callback_function(
xmlhttp.responseText);
else if((xmlhttp.status!==200) ||
xmlhttp.status===200 &&
xmlhttp.readyState!==2 &&
xmlhttp.readyState!==3)
alert("readystate "+xmlhttp.readyState
+" status "+xmlhttp.status);}
xmlhttp.open("GET",
this.server_location+
this.server_file+
this.query_parameters,
true);
xmlhttp.send();}
Generic_server_request.prototype
.set_query_parameters=function(query_parameters){
this.query_parameters=query_parameters}
function server_querry(input){
if(query_request_manager === undefined){
query_request_manager=new Generic_server_request(
"http://localhost/cgi-bin/",
"querry_handler.php",
status_text);}
query_request_manager.set_query_parameters("?input="+input);
query_request_manager.server_request();}
问题是,当服务器响应到达时,我在firebug控制台中得到了这个错误:"this。Client_callback_function不是一个函数"。我可以使用一些帮助来理解作用域发生了什么。
编辑我有一个以前的版本,它做了所有应该实现的通用…在以前版本的server_query (input)和status_text中,server_query (input)在ATM代码的其他部分运行得很好,所以这不是问题。使用server_status和server_status的代码与之前预期的功能没有任何修改。
也可以像这样工作:
函数Generic_server_request(…,client_callback_function) {...this.client_callback_function = client_callback_function;client_callback_function("测试");//工作this.client_callback_function("测试");//作品
创建请求对象时使用:
new Generic_server_request(
"http://localhost/cgi-bin/",
"querry_handler.php",
status_text
);
但是在构造函数中你的函数描述为:
function Generic_server_request(
server_location,
server_file,
client_callback_function
)
你传递的不是client_callback_function
而是status_text
(这不是一个函数)
我解决了我最初的问题,虽然我创建了另一个问题,虽然这是好的另一个问题,如果我没有得到新的建议。这是我目前相对于上述代码的一般外观的出发点(顶部代码块):
function global_function(server_response){}
function Constructor(global_function){
this.local_reference_to_global_function=global_function;}
//still doesn't work but now I intuitively get why it doesn't work
//apparently this.xmlhttp is created new instead of being the one in api_function
//and the 2 are not shared through the Constructor(global_function) environment
//I could use some help from someone that would tell me
//how to make minimal changes to get this.xmlhttp in this example
//(this.xmlhttp in prototype.callback and prototype.api_function)
//to reference the same object
//or make equivalent minimal changes to other parts of the code
Constructor.prototype.callback=function(){
this.xmlhttp.local_reference_to_global_function(this.xmlhttp.responseText);};
Constructor.prototype.api_function=function(){
this.xmlhttp=new XMLHttpRequest();
this.xmlhttp.onreadystatechange=this.callback();
//xmlhttp.onreadystatechange=function(){
// this.local_reference_to_global_function(xmlhttp.responseText)}
// old version accesses local_reference_to_global_function
// inside onreadystatechange function's new environment a new object
// which is where I was stumped initially
// new variable environment created that I didn't get
xmlhttp.open("GET",...);
xmlhttp.send();}
var derivativeConstructor;
function server_query(){
if(derivativeConstrucror===undefined){
var derivativeConstructor=new Constructor(global_function);}
derivativeConstructor.api_function();}
- 带Jquery的wep API,加载资源失败:服务器响应状态为404(未找到)
- 如何在 http 上调用 end().服务器响应对象
- 将服务器响应格式化为人类可读
- 尝试获取服务器响应
- 如何在 Javascript 函数中拦截 Web 服务器响应
- 如何从服务器响应中显示phonegap中的图像
- Nodejs-websocket:如何在没有服务器响应的情况下连续发送客户端消息
- 正在分析Meteor中的错误服务器响应
- 正在从LWIP服务器响应中检索16位整数
- 可以´t从对象XMLHttpRequest获取服务器响应
- 试图理解返回javascript方法的服务器响应
- AJAX 中的常见服务器响应页原因
- 节点服务器响应错误: process.nextTick(function(){throw err;});.
- GO 中的长时间轮询,服务器响应不当
- 在 Iframe 中发布服务器响应
- Jquery 响应从服务器响应中修剪值
- 在铁表单提交后检查服务器响应
- 量角器单击链接并将服务器响应与文件进行比较
- 如何确定 Node .js服务器响应是否已停止
- 当服务器响应缓慢时,jQuery 在加载/就绪时不触发