weixin_33670786 2017-03-21 18:40 采纳率: 0%
浏览 22

AJAX JSON调用失败[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element" dir="ltr">Why does jQuery or a DOM method such as getElementById not find the element?</a>
                            <span class="question-originals-answer-count">
                                (9 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2017-03-21 18:59:10Z" class="relativetime">3 years ago</span>.</div>
        </div>
    </aside>

I am doing a AJAX call pulling JSON data from a specific site but for some reason I am receiving and error before the call.

Error message:

Uncaught TypeError: Cannot read property 'addEventListener' of null at script.js:2

Code:

var ajaxtest = document.getElementById("dmeo");

ajaxtest.addEventListener("click",function() {
var ourRequest = new XMLHttpRequest();
ourRequest.open("GET","https://learnwebcode.github.io/json-example/animals-1.json")//post if you want to send data
ourRequest.onload = function(){
   // console.log(ourRequest.responseText);//test logging to console.
var ourData = JSON.parse(ourRequest.responseText);
console.log(ourData[0]);
};
ourRequest.send();
});
</div>
  • 写回答

2条回答 默认 最新

  • weixin_33721344 2017-03-21 18:43
    关注

    typo? var ajaxtest = document.getElementById("dmeo"); should be "demo"?

    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波