2017-02-04 5 views
0

私はJSONから値を取得し、Ajaxリクエストの内側に、関数内で使用したい:機能 - コルドバ/ Framework7

​​

AJAXリクエスト:

$$.ajax({     
        type: "GET", 
        dataType: 'json', 
        url: target, 
        //Sucess 
        success: function(data){ 
        var img_url = data.media.display_src; 
        Hi(img_url); 
        }, 

        //Error 
       error: function(xhr,status){ 
        alert("Error"+status+xhr); 
        console.log(xhr.response); 
       } 
      }); 

しかし、関数Hi()は常に「不確定」です...何が問題なのですか?

おかげ

答えて

3

あなたHi機能は、別のスコープ内にあるので。すべてfunction自身のスコープを作成します。したがって、scopeで定義されているものはundefinedです。関数をonDeviceReady関数から移動します。ここで

あなたは彼らがouter機能の外に表示されていないので、innerFunctioninnerVariableは、undefinedていることがわかります。

function outer(){ 
 
    
 
    var innerVariable = 'hello'; 
 
    
 
    function innerFunction(){ 
 
    console.log('inner function') ; 
 
    } 
 
    
 
} 
 

 
console.log(innerVariable); 
 
innerFunction();

+0

ハム...右...私はこれに気づいていなかった....ありがとう:) –

0

だけonDeviceReady()関数の外Hi()を定義します。

document.addEventListener("deviceready", onDeviceReady, false); 
function Hi(img_url){ 
    alert('Hi, the img_url is '+img_url); 
} 
function onDeviceReady() { 
    alert('device ready'); 

} 

JavaScriptでは、定義されたスコープ外の関数を使用することはできません。ここにJSの範囲の詳細な説明があります:What is the scope of variables in JavaScript?

+0

私はこれを読むでしょう..ありがとう:) –