Javascript 检索JS函数值
我目前正在尝试检索一个JS返回值,但我真的不知道它不起作用的原因 你好,希望我的代码是最容易阅读的,提前感谢:Javascript 检索JS函数值,javascript,jquery-mobile,Javascript,Jquery Mobile,我目前正在尝试检索一个JS返回值,但我真的不知道它不起作用的原因 你好,希望我的代码是最容易阅读的,提前感谢: <script type="text/javascript"> function getValLocalSto(key, URL){ console.log(key); console.log(URL); var myKey
<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>
您可以将回调传递给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
});
只需在ajax success函数外部声明测试变量,在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>
函数getValLocalSto(键,URL){
...
if(myKey==null){
//获取JSON数据
var检验;
$.ajax({
url:url,
数据类型:“json”,
async:false,
成功:函数(json){
console.log(JSON.stringify(JSON));//结果是格式良好的JSON字符串
setItem(key,JSON.stringify(JSON));
myKey=localStorage.getItem(key);
test=jQuery.parseJSON(myKey);
log(“我的函数结果:+test);//结果是一个[object]
}
});
回归试验;
}否则{
//其他工作
}
}
//调用我的函数
log(“出击次数:+getValLocalSto(“testJson”,“do/profil”);//结果是“未定义”
...
您需要阅读javascript中的异步基础知识。简言之,当返回值时,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>