调用在函数内声明的变量
call a variable that is declared within a function
我在 chrome 扩展中使用消息传递将数据从 background.js 移动到 content.js 文件。
在下面的函数中,当我在函数中将变量输出到控制台时,我得到了我期望的数据。当我在函数外尝试相同的操作时,我得到 null(如全局变量中所定义)。
有谁知道我如何从函数中提取数据,以便我可以通过 AJAX 调用 post 它。
代码
var capturedData = "url=" + window.location.href;
var localHash = "localhash=" + CryptoJS.MD5(capturedData);
var email = null;
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
email = response;
console.log(JSON.stringify(email));
return true;
});
console.log(JSON.stringify(email));
$.ajax({
type: 'post',
url: 'url',
data: {"url":capturedData, "localhash":localHash},
dataType: 'json',
success : function (data) {
if(data.url)
{
console.log(data.url);
}
}
});
输出
null content.js:10 {"email":"email@email.com"} content.js:7
谢谢!
您需要在通过电子邮件获得一些价值后拨打 ajax 电话:
var capturedData = "url=" + window.location.href;
var localHash = "localhash=" + CryptoJS.MD5(capturedData);
var email = null;
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
email = response;
console.log(JSON.stringify(email));
$.ajax({
type: 'post',
url: 'url',
data: {"url":capturedData, "localhash":localHash},
dataType: 'json',
success : function (data) {
if(data.url)
{
console.log(data.url);
}
}
});
return true;
});
console.log(JSON.stringify(email));
我在 chrome 扩展中使用消息传递将数据从 background.js 移动到 content.js 文件。
在下面的函数中,当我在函数中将变量输出到控制台时,我得到了我期望的数据。当我在函数外尝试相同的操作时,我得到 null(如全局变量中所定义)。
有谁知道我如何从函数中提取数据,以便我可以通过 AJAX 调用 post 它。
代码
var capturedData = "url=" + window.location.href;
var localHash = "localhash=" + CryptoJS.MD5(capturedData);
var email = null;
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
email = response;
console.log(JSON.stringify(email));
return true;
});
console.log(JSON.stringify(email));
$.ajax({
type: 'post',
url: 'url',
data: {"url":capturedData, "localhash":localHash},
dataType: 'json',
success : function (data) {
if(data.url)
{
console.log(data.url);
}
}
});
输出
null content.js:10 {"email":"email@email.com"} content.js:7
谢谢!
您需要在通过电子邮件获得一些价值后拨打 ajax 电话:
var capturedData = "url=" + window.location.href;
var localHash = "localhash=" + CryptoJS.MD5(capturedData);
var email = null;
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
email = response;
console.log(JSON.stringify(email));
$.ajax({
type: 'post',
url: 'url',
data: {"url":capturedData, "localhash":localHash},
dataType: 'json',
success : function (data) {
if(data.url)
{
console.log(data.url);
}
}
});
return true;
});
console.log(JSON.stringify(email));