调用函数中声明的变量
call a variable that is declared within a function
我在chrome扩展中使用消息传递将数据从background.js移动到content.js文件。
在下面的函数中,当我将变量输出到函数中的控制台时,我会得到我期望的数据。当我在函数外尝试相同的操作时,我会得到null(如全局变量中定义的)。
有人知道我如何从函数中提取数据,这样我就可以用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));
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.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));
相关文章:
- 使用同一对象中的其他变量声明变量
- 如何将变量声明为全局变量
- 如何将变量声明为全局变量?用case foreach?Javascript
- 理解bootstrap.js变量声明
- 将 Javascript 变量声明为 PHP 形式
- 单个逗号分隔的变量声明和多个声明之间的区别是什么
- Do变量声明初始化函数
- Javascript变量声明:什么是“;var myVariable={}”;
- javascript中不同类型的变量声明
- 为什么在循环的Javascript中包含变量声明
- for 循环中必需的变量声明
- JavaScript 中的变量声明
- 在 Javascript 变量声明中使用逗号
- 你能在变量声明中添加条件吗?
- JavaScript 全局变量声明
- 节点.js变量声明和作用域
- 具有多个逗号分隔值的变量声明是什么意思(例如 var a = b,c,d;)
- 移动图像 ..JavaScript 将变量声明为字符串
- 让 javascript 将(默认情况下)未声明的变量声明到当前本地范围(基于首次使用)而不是全局范围
- 循环中的变量声明