简体   繁体   English

getjson什么都没发生

[英]getjson nothing happening

Having troubles with the following code. 遇到以下代码的麻烦。 At this point I'd like to see the alert but not even that is happening. 在这一点上,我想看到警报,但即便如此。

$.getJSON('demo.js',function(result){
        alert(1);
      $.each(result, function(i, field){
        $("div").append(field + " ");
      });
    });

demo.js looks like: demo.js看起来像:

{ 
  "firstName": "John",
  "lastName": "Doe",
  "age": 25
}

That alert isn't even popping up. 这种警报甚至没有出现。 This is code taken from a web tutorial and it's not working for me. 这是从网络教程中获取的代码,它对我不起作用。 I'm pretty stumped. 我很难过。 Any ideas? 有任何想法吗?

For debug purpose, you can do 出于调试目的,您可以这样做

$.getJSON('demo.js',function(result) {
   console.log(result);
}).fail(function (j, t, e) {
   console.error(e);
});

But I think your problem is either invalid json or header. 但我认为您的问题是无效的json或标头。

In order to $.getJSON, you have to submit a json datatype 为了$ .getJSON,您必须提交json数据类型

Using longhand $.ajax would work i believe if you were to omit datatype, but theres really no reason unless by your own restriction that demo sold be a js instead of a json file 使用longhand $ .ajax会工作我相信如果你要省略数据类型,但除非你自己的限制,所以出售的demo是js而不是json文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM