检索JS函数值
Retrieve JS function value
我目前正在尝试检索JS返回值,我真的不知道为什么它不工作的原因…
嗨,希望我的代码是最容易读的,提前感谢:
<script type="text/javascript">
function getValLocalSto(key, URL){
console.log(key);
console.log(URL);
var myKey = localStorage.getItem(key);
if(myKey == null){
// getting back JSON data
$.ajax({
url: URL,
dataType: 'json',
async: false,
success: function (json) {
var test;
console.log(JSON.stringify(json)); // the result is a well formed JSON string
localStorage.setItem(key,JSON.stringify(json));
myKey = localStorage.getItem(key);
test =jQuery.parseJSON(myKey);
console.log("My function result : "+test); // the result is a [object Object]
return test;
}
});
}else {
// Other work whatever
}
}
//call my function
console.log("sortie fonction : "+getValLocalSto("testJson", "do/profil")); // the result is "undefined"
var myuResult = getValLocalSto("testJson", "do/profil")); // the result is "undefined"
console.log(ff.prenom);
document.getElementById("myDiv").innerHTML="<div><input disabled='disabled' name='text-basic' id='text-basic' type= 'text' value='Votre nom "+ff.nom+"'/></div>";
document.getElementById("myDiv").innerHTML+="<div> <input disabled='disabled' name='text-basic' id='text-basic' type= 'text' value= 'Votre prenom "+ff.prenom+"'/></div>";
</script>
解决方案:
function getValLocalSto(key, URL){
// do my stuff
});
}else {
// Other work whatever
}
return test;
}
只需在ajax成功函数之外声明测试变量在getValLocalSto外部函数中利用变量作用域。否则,您将需要一个回调来从ajax成功函数返回变量。试试这个:
<script type="text/javascript">
function getValLocalSto(key, URL){
...
if(myKey == null){
// getting back JSON data
var test;
$.ajax({
url: URL,
dataType: 'json',
async: false,
success: function (json) {
console.log(JSON.stringify(json)); // the result is a well formed JSON string
localStorage.setItem(key,JSON.stringify(json));
myKey = localStorage.getItem(key);
test =jQuery.parseJSON(myKey);
console.log("My function result : "+test); // the result is a [object Object]
}
});
return test;
}else {
// Other work whatever
}
}
//call my function
console.log("sortie fonction : "+getValLocalSto("testJson", "do/profil")); // the result is "undefined"
...
</script>
可以向AJAX传递回调。将函数定义更改为:
function getValLocalSto(key, URL, callback){
...
$.ajax({
url: URL,
dataType: 'json',
async: false,
success: callback
});
...
代码中的
getValLocalSto("testJson", "do/profil", function(data) {
localStorage.setItem(key,JSON.stringify(data));
myKey = localStorage.getItem(key);
test = jQuery.parseJSON(myKey);
// here you work with test as you want as a result of getValLocalSto
});
相关文章:
- 为什么我的控制器没有启动函数.js脚本
- 在函数js中传递变量
- 如何在单独的文件中调用函数 js.
- 在铯中调用函数.js当事件被尝试时
- 只有setTimeout执行函数.JS
- 为什么图像选择器程序中的这个函数 js 不起作用
- 获取邮件中的信息.php从函数.js解析为manage.html
- 如何从应用程序中调用JS函数.js在Rails4视图中
- Uncatch TypeError: obj.attr 不是函数 JS
- 从 C++ 节点调用 JavaScript 函数.js扩展
- 调用 Backbone 内部的外部函数.js的视图初始化方法
- 如何使用 Scala 创建 JavaScript 函数.js
- 使用带有下划线循环的回调函数.js骨干.js项目不起作用
- 获取字段值的函数-JS
- 函数js不能递归调用自己的函数
- 在函数JS中创建带有地理代码的标记
- 循环内部的函数(js/jquery)
- 我需要帮助将此代码添加到jQuery函数.js文件中
- 未捕获类型:未定义不是函数JS错误
- 停止多个内联函数JS的事件传播