从外部javascript调用时,getJSON url方法不起作用

时间:2014-11-15 05:43:52

标签: javascript jquery ajax json jsonp

这是一个复杂的问题,我想知道这是否与JSONP(JSON +填充)更相关。

所以有一个名为example.php的文件

<script src="jquery.js"</script>
<script src="example.js" onload="getInfo();"></script>

<p class="one"></p>

现在在example.js文件中,我使用了getJSON方法:

$(document).ready(function() {
   function getInfo() {
        $.getJSON("URL", function(data) {
        var name = [parses through the json data];
        $("p.one").html(name);

但这不起作用,但如果它不涉及外部JSON文件则可以正常工作。 以上是之前回答的问题:Best way to access external JavaScript file and place contents in div? 很想知道是否有其他人遇到过这个问题而且我还没有能够通过谷歌或StackOverflow找到任何东西。

1 个答案:

答案 0 :(得分:0)

更改脚本调用语法,如下所示。删除onload attribue。 onload代码

没有属性script
<script src="example.js"></script>

而且,您的example.js文件看起来像这样。您也可以在该文件中调用getInfo()方法

$(document).ready(function() {
   function getInfo() {
        $.getJSON("URL", function(data) {
        var name = [parses through the json data];
        $("p.one").html(name);
        });
   }

   getInfo();
});
相关问题