Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery Ajax请求(CORS)返回未定义_Javascript_Jquery_Ajax_Cors_Cross Domain - Fatal编程技术网

Javascript jQuery Ajax请求(CORS)返回未定义

Javascript jQuery Ajax请求(CORS)返回未定义,javascript,jquery,ajax,cors,cross-domain,Javascript,Jquery,Ajax,Cors,Cross Domain,这是我第一次尝试向API发出跨域Ajax请求。我正在使用jQuery进行调用,并尝试将返回中的某些项目放到页面上 请求如下: $.ajax({ type: 'POST', url: 'http://magicseaweed.com/api/APIKEY/forecast/?spot_id=665', contentType: "text/plain", dataType: "json", xhrFields: { withCrede

这是我第一次尝试向API发出跨域Ajax请求。我正在使用jQuery进行调用,并尝试将返回中的某些项目放到页面上

请求如下:

$.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"
        }
    }
}]
目前,我正在尝试将时间戳字符串显示在警报框中

警报(时间戳)返回为未定义。我的错误在哪里

        timestamp = data[0].localTimestamp;
不是

因为响应是一个包含一个项的数组
[{…}]

不是


因为响应是一个包含一个项的数组
[{…}]

警报(数据)
的输出是什么,或者更好的
控制台.log(JSON.stringify(data))
-nvm,只是看到了您的数据结构@Abdennour TOUMI的回答是正确的,看起来响应是一个数组。您是否尝试过
数据[0]。localTimestamp
警报(数据)的输出是什么,或者更好的
控制台.log(JSON.stringify(数据))
-nvm,刚刚看到了您的数据结构@Abdennour TOUMI的回答是正确的,看起来响应是一个数组。您是否尝试过
数据[0]。localTimestamp
        timestamp = data.localTimestamp;