gpt4 book ai didi

javascript - 如何从jquery ajax成功内部调用parents方法

转载 作者:行者123 更新时间:2023-11-28 13:47:07 25 4
gpt4 key购买 nike

Possible Duplicate:
jquery reference this from parent

这是我在这里发表的第一篇文章。问题 - 如何从 ajax-success 调用警报?

var page = {

alerts: function (json) {

if (json == 'us') { alert("blablabla") } else {
alert(json.login);
}
},

ajax: function (obj) {

$.ajax({
type: 'post',
url: './scripts/serv.php',
dataType: 'json',
data: { object: $.base64.encode(JSON.stringify(obj)) },

success: function (json) {

// how to call from here alerts(json)?? this.alerts(json) - doesn't
// work only page.alerts(json) but i think its wrong way.

},
error: function (json) {
alert('ajax failed');
}
})
}
}

最佳答案

您应该在 AJAX 请求之前存储对此的引用,因为 success 是从不同的范围调用的。

示例:

var page = {
alerts: function(json){
if (json=='us'){ alert("blablabla")} else{
alert(json.login); }
},
ajax: function(obj){
var me = this;

$.ajax({
type:'post',
url:'./scripts/serv.php',
dataType:'json',
data:{object:$.base64.encode(JSON.stringify(obj))},

success: function(json){
// how to call from here alerts(json)?? this.alerts(json) - doesn't work only page.alerts(json) but i think its wrong way.
me.alerts(json);
},
error: function(json){
alert('ajax failed');
}
});
}
};

关于javascript - 如何从jquery ajax成功内部调用parents方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13493982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com