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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)