这是我第一次使用AJAX,我确定其他人有这个错误,但经过大量的尝试和搜索后我找不到答案。我相信这很简单。我会很感激任何帮助。当记录AJAX/JSON请求时,Console.log语句显示为undefined
// create variable for JSON request
var ourRequest = new XMLHttpRequest();
// get request for tested JSON link
ourRequest.open('GET', 'http://api.fixer.io/latest');
ourRequest.onload = function() {
var ourData = JSON.parse(ourRequest.responseText);
console.log(ourData[1]);
};
ourRequest.send();
所以它显示为未定义,当我运行在眉毛呃并检查控制台。我使用Chrome –
api返回一个对象'{“base”:“EUR”,“date”:“2017-02-24”,...'而不是数组。 - > [MDN:使用对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects),[fiddle](https://jsfiddle.net/17x5cgfb/) – Andreas
这是一个跨源请求吗?什么是'.responseText'?什么是'.status'?什么是'ourData'(而不是'ourData [1]')? – Bergi