Sorry if this question is duplicated but I couldn't solve my problem from other solutions.
很抱歉,如果这个问题重复,但我无法解决其他解决方案中的问题。
I've got this code in a sepate file included in my main index:
我在我的主索引中包含的sepate文件中有这个代码:
var getSuggestedData = {
serviceURL: $("input[name=suggestedServices]").val(),
dataR:"",
doRequest:function(){
//request data to controller
$.ajax({
url:this.serviceURL,
success:function(msg){
this.dataR = msg;
}
})
}
}
}
When I'm trying to get the variable "dataR" from my index this way it's UNDEFINED! PLEASE, can someone help me out?
当我试图通过这种方式从我的索引中获取变量“dataR”时,它是未定义的!请问,有人可以帮帮我吗?
$().ready(function() {
getSuggestedData.doRequest();
alert(getSuggestedData.dataR);
});
});
Thank you in advance!
先谢谢你!
2 个解决方案
#1
0
The reason you are not able to access the dataR object is because it is not in the same context as the result returned from the success method.
您无法访问dataR对象的原因是因为它与success方法返回的结果不在同一上下文中。
One technique is to hold a reference to this in a variable as shown below:
一种技术是在变量中保存对此的引用,如下所示:
var self = this;
using the jquery library!
$(this.button).bind('click',{self:this},function(event)
{
var that = event.data.self;
alert(that.num);
});
You can also check out the post below in which I explained in detailed about the "this" keyword.
您还可以查看下面的帖子,我在其中详细介绍了“this”关键字。
http://azamsharp.com/Posts/57_I_mean__this__not__this_.aspx
http://azamsharp.com/Posts/57_I_mean__this__not__this_.aspx
#2
0
If memory serves me right...
如果记忆对我有用......
this.dataR = msg;
probably needs to be
可能需要
getSuggestedData.dataR = msg
the 'this' reference would be to the object fed to jQuery, you need to reference the original object. I forget if you could access it by its name directly such as this or if you need to use another method, let me know if it doesn't work out though.
'this'引用将是提供给jQuery的对象,需要引用原始对象。我忘记了你是否可以通过它的名字直接访问它,或者如果你需要使用其他方法,请告诉我它是否有效。
#1
0
The reason you are not able to access the dataR object is because it is not in the same context as the result returned from the success method.
您无法访问dataR对象的原因是因为它与success方法返回的结果不在同一上下文中。
One technique is to hold a reference to this in a variable as shown below:
一种技术是在变量中保存对此的引用,如下所示:
var self = this;
using the jquery library!
$(this.button).bind('click',{self:this},function(event)
{
var that = event.data.self;
alert(that.num);
});
You can also check out the post below in which I explained in detailed about the "this" keyword.
您还可以查看下面的帖子,我在其中详细介绍了“this”关键字。
http://azamsharp.com/Posts/57_I_mean__this__not__this_.aspx
http://azamsharp.com/Posts/57_I_mean__this__not__this_.aspx
#2
0
If memory serves me right...
如果记忆对我有用......
this.dataR = msg;
probably needs to be
可能需要
getSuggestedData.dataR = msg
the 'this' reference would be to the object fed to jQuery, you need to reference the original object. I forget if you could access it by its name directly such as this or if you need to use another method, let me know if it doesn't work out though.
'this'引用将是提供给jQuery的对象,需要引用原始对象。我忘记了你是否可以通过它的名字直接访问它,或者如果你需要使用其他方法,请告诉我它是否有效。