当使用getJSON时,回调函数不起作用。

时间:2022-08-15 19:41:36

This is the code that I am using, When I write the link into the browser (I.E. or Mozilla) it is working like (MyFunc({"memes":[{"source":"http://www.knall......), but when I try to run it as HTML file I have a error in status Bar. what is the problem?. Thanks

这是我使用的代码,当我将链接写入浏览器(例如,或Mozilla)时,它就像(MyFunc) (MyFunc({“memes”:{“源”:http://www.knall…),但是当我试图将它作为HTML文件运行时,我在状态栏中有一个错误。这个问题是什么?。谢谢

<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
    <div id="images"></div>
<script>$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc",function(data){         
               alert(data);
        });
</script>
</body>

4 个解决方案

#1


4  

You don't define MyFunc anywhere in your code. You should rather put a ? in the URL instead of an arbitrary name, and jQuery will replace it with a generated callback name.

在代码的任何地方都不定义MyFunc。你应该写一个?在URL中而不是任意名称中,jQuery将用生成的回调名称替换它。

#2


0  

Eureka man! It doesn't work with the latest version... you should use the jquery 1.3.1 not newer...

尤里卡男人!它与最新版本不兼容……您应该使用jquery 1.3.1,而不是更新的……

#3


-1  

You have to use getScript instead of getJSON since you are calling a URL on another domain name.

您必须使用getScript而不是getJSON,因为您正在调用另一个域名的URL。

Update:

更新:

The following code works fine for me:

下面的代码对我来说很有用:

<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
    <div id="images"></div>
    <script>
        function MyFunc(data) {
            alert(data)
        }
        $.getScript("http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc");
</script>
</body>

#4


-1  

you cant make ajax calls to other domains

不能对其他域进行ajax调用

http://en.wikipedia.org/wiki/Same_origin_policy

http://en.wikipedia.org/wiki/Same_origin_policy

Also, your URL is not a valid url, copy and paste it in a browser and you will see an error http://tagthe.net/api/url=http://www.knallgrau.at/en&view=json&callback=MyFunc

此外,您的URL不是一个有效的URL,复制并粘贴到浏览器中,您将看到一个错误http://tagthe.net/api/url=http://www.knallgrau.at/en&view=json&callback=MyFunc

your valid URL is: http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc

您的有效URL是:http://tagthe.net/api/?url=http:// /www.knallgrau.at/en&view=json&callback=MyFunc

$.getJSON("
    http://tagthe.net/api/url=http://www.knallgrau.at/en&view=json&callback=MyFunc",
    function(data){         
        alert(data);
    });

#1


4  

You don't define MyFunc anywhere in your code. You should rather put a ? in the URL instead of an arbitrary name, and jQuery will replace it with a generated callback name.

在代码的任何地方都不定义MyFunc。你应该写一个?在URL中而不是任意名称中,jQuery将用生成的回调名称替换它。

#2


0  

Eureka man! It doesn't work with the latest version... you should use the jquery 1.3.1 not newer...

尤里卡男人!它与最新版本不兼容……您应该使用jquery 1.3.1,而不是更新的……

#3


-1  

You have to use getScript instead of getJSON since you are calling a URL on another domain name.

您必须使用getScript而不是getJSON,因为您正在调用另一个域名的URL。

Update:

更新:

The following code works fine for me:

下面的代码对我来说很有用:

<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
    <div id="images"></div>
    <script>
        function MyFunc(data) {
            alert(data)
        }
        $.getScript("http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc");
</script>
</body>

#4


-1  

you cant make ajax calls to other domains

不能对其他域进行ajax调用

http://en.wikipedia.org/wiki/Same_origin_policy

http://en.wikipedia.org/wiki/Same_origin_policy

Also, your URL is not a valid url, copy and paste it in a browser and you will see an error http://tagthe.net/api/url=http://www.knallgrau.at/en&view=json&callback=MyFunc

此外,您的URL不是一个有效的URL,复制并粘贴到浏览器中,您将看到一个错误http://tagthe.net/api/url=http://www.knallgrau.at/en&view=json&callback=MyFunc

your valid URL is: http://tagthe.net/api/?url=http://www.knallgrau.at/en&view=json&callback=MyFunc

您的有效URL是:http://tagthe.net/api/?url=http:// /www.knallgrau.at/en&view=json&callback=MyFunc

$.getJSON("
    http://tagthe.net/api/url=http://www.knallgrau.at/en&view=json&callback=MyFunc",
    function(data){         
        alert(data);
    });