2013-04-26 40 views
0

我目前正試圖檢索JS返回值,我真的不知道爲什麼振振有辭它不工作...檢索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; 
       } 
+0

您需要閱讀javascript中的異步基礎知識。簡而言之 - 當返回值返回時,ajax調用還沒有完成。 – David 2013-04-26 10:08:16

+0

好的,謝謝我找到解決方案並重新編輯我的動作。 – Lombric 2013-04-26 10:16:58

+0

是達到成功的方法嗎? – 2013-04-26 10:17:17

回答

0

只要申報測試變量的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> 
0

你可以傳遞一個回調到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 
});