我在Chrome插件中使用以下代码来制作跨源请求并将其保存到本地存储:
function Search(query) {
this.query = query;
this.fetch = function(callback) {
if (query) {
console.log(query);
// new cross origin XML request
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
this.searchResult = JSON.parse(xmlhttp.responseText);
console.log(this.searchResult);
callback(this.searchResult);
}
else
callback("noquery");
}
};
xmlhttp.open("GET", "xxxxxxx" + this.query, true);
xmlhttp.send();
}
else
callback("noquery");
};
this.save = function(results) {
chrome.storage.local.set({'searchResults': results});
console.log('results saved');
// save results
};
我正在尝试使用以下代码执行并传递searchResults.save()作为回调函数:
searchResults = new Search(currentSearchURL.query());
searchResults.fetch(searchResults.save());
但是我在控制台中收到以下错误:
Uncaught TypeError: object is not a function background.js:35
xmlhttp.onreadystatechange
有谁看到我在这里做错了什么?