0
这是我第一次尝试向API发出跨域Ajax请求。我正在使用jQuery进行调用,并试图将返回的某些项目放到页面上。jQuery Ajax请求(CORS)返回undefined
这里的要求:
$.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);
由于未定义而返回。我的错误在哪里?
数组什么警报'输出(数据)',甚至更好'的console.log(JSON.stringify (data))' - nvm,刚刚看到你的数据结构。 @Abdennour TOUMI答案正确 – JorgeObregon
看起来响应是一个数组。你有没有试过'data [0] .localTimestamp' –