聚合物!调用函数 Ajax 成功
Polymer! Call a function in Ajax success
我只是不知道如何在 ajax 成功中调用另一个聚合物函数。
这是我的代码:
<script>
(function () {
// these variables are shared by all instances of app-globals
var firstName = "John";
function setData(data) {
firstName = data;
console.log("Data gesetzt:" + firstName);
}
Polymer({
ready: function () {
this.setTest("Example"); //Works
$.ajax({
async: false,
type: "GET",
url: "http://www.jsv-lippstadt.de/?json=get_category_posts&slug=" + this.from,
dataType: 'jsonp',
error: function () {
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function (data) {
this.setTest(); //Doesnt work
}
});
},
setTest: function () {
console.log("hi");
}
});
})();
</script>
这是成功函数的控制台日志:未捕获类型错误:未定义不是函数。
那么如何在我的回调中调用 setTest: function(){}?
您需要为 jQuery ajax 调用设置上下文选项,以便成功处理程序中的 this
具有正确的值(它通常会指向 jqXHR 对象) :
Polymer({
ready: function () {
this.setTest("Example"); //Works
$.ajax({
// set the context option so this will have the desired value
// in the success handler
context: this,
async: false,
type: "GET",
url: "http://www.jsv-lippstadt.de/?json=get_category_posts&slug=" + this.from,
dataType: 'jsonp',
error: function () {
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function (data) {
this.setTest(); // should work now
}
});
},
setTest: function () {
console.log("hi");
}
或者,您可以像这样将 this
值保存到另一个变量中,然后可以从回调中使用该变量:
Polymer({
ready: function () {
// save this value into another variable so it can be used in callbacks
var self = this;
this.setTest("Example"); //Works
$.ajax({
async: false,
type: "GET",
url: "http://www.jsv-lippstadt.de/?json=get_category_posts&slug=" + this.from,
dataType: 'jsonp',
error: function () {
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function (data) {
self.setTest(); // should work now
}
});
},
setTest: function () {
console.log("hi");
}
我只是不知道如何在 ajax 成功中调用另一个聚合物函数。
这是我的代码:
<script>
(function () {
// these variables are shared by all instances of app-globals
var firstName = "John";
function setData(data) {
firstName = data;
console.log("Data gesetzt:" + firstName);
}
Polymer({
ready: function () {
this.setTest("Example"); //Works
$.ajax({
async: false,
type: "GET",
url: "http://www.jsv-lippstadt.de/?json=get_category_posts&slug=" + this.from,
dataType: 'jsonp',
error: function () {
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function (data) {
this.setTest(); //Doesnt work
}
});
},
setTest: function () {
console.log("hi");
}
});
})();
</script>
这是成功函数的控制台日志:未捕获类型错误:未定义不是函数。
那么如何在我的回调中调用 setTest: function(){}?
您需要为 jQuery ajax 调用设置上下文选项,以便成功处理程序中的 this
具有正确的值(它通常会指向 jqXHR 对象) :
Polymer({
ready: function () {
this.setTest("Example"); //Works
$.ajax({
// set the context option so this will have the desired value
// in the success handler
context: this,
async: false,
type: "GET",
url: "http://www.jsv-lippstadt.de/?json=get_category_posts&slug=" + this.from,
dataType: 'jsonp',
error: function () {
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function (data) {
this.setTest(); // should work now
}
});
},
setTest: function () {
console.log("hi");
}
或者,您可以像这样将 this
值保存到另一个变量中,然后可以从回调中使用该变量:
Polymer({
ready: function () {
// save this value into another variable so it can be used in callbacks
var self = this;
this.setTest("Example"); //Works
$.ajax({
async: false,
type: "GET",
url: "http://www.jsv-lippstadt.de/?json=get_category_posts&slug=" + this.from,
dataType: 'jsonp',
error: function () {
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function (data) {
self.setTest(); // should work now
}
});
},
setTest: function () {
console.log("hi");
}