2017-02-08 6 views
0

これは、クロスドメインAjaxリクエストをAPIに作成する私の最初の試みです。私は呼び出しを行うためにjQueryを使用しており、ページから返された特定の項目を配置しようとしています。ここでjQuery Ajaxリクエスト(CORS)が未定義に戻る

$.ajax({ 
    type: 'POST', 
    url: 'http://magicseaweed.com/api/APIKEY/forecast/?spot_id=665', 
    contentType: "text/plain", 
    dataType: "json", 
    xhrFields: { 
      withCredentials: false 
    }, 
    success: function(data) { 
      timestamp = data.localTimestamp; 
      alert(timestamp); 
    }, 
    error: function() { 
      alert("aw crap"); 
    } 
}); 

をレスポンスです:

は、ここでリクエストが現在、私はちょうど警告ボックスに表示するタイムスタンプ文字列を取得しようとしている

[{ 
    timestamp: 1366902000, 
    localTimestamp: 1366902000, 
    issueTimestamp: 1366848000, 
    fadedRating: 0, 
    solidRating: 0, 
    swell: { 
     minBreakingHeight: 1, 
     absMinBreakingHeight: 1.06, 
     maxBreakingHeight: 2, 
     absMaxBreakingHeight: 1.66, 
     unit: "ft", 
     components: { 
      combined: { 
      height: 1.1, 
      period: 14, 
      direction: 93.25, 
      compassDirection: "W" 
     }, 
     primary: { 
      height: 1, 
      period: 7, 
      direction: 83.37, 
      compassDirection: "W" 
     }, 
     secondary: { 
      height: 0.4, 
      period: 9, 
      direction: 92.32, 
      compassDirection: "W" 
     }, 
     tertiary: { 
      height: 0.3, 
      period: 13, 
      direction: 94.47, 
      compassDirection: "W" 
     } 
    } 
}] 

alert(timestamp);は未定義として返されます。私の誤りはどこですか?

+0

を持つ配列であるため、 (データ)) ' - nvm、ちょうどあなたのデータ構造を見た。 @Abdennour TOUMIの答えが正しい – JorgeObregon

+0

それは応答が配列であるように見えます。あなたは 'data [0] .localTimestamp'を試しましたか? –

答えて

3
 timestamp = data[0].localTimestamp; 

ない

 timestamp = data.localTimestamp; 

応答は `警告(データ)`の出力だ、あるいはさらに良い `はconsole.log(JSON.stringify何つの項目[{...}]

関連する問題