开发者

Callback function doesn't work when using getJSON

开发者 https://www.devze.com 2022-12-30 22:04 出处:网络
This is the code that I am using, When I write the link into the browser (I.E. or Mozilla) it is working like

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

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


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.


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


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

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>


you cant make ajax calls to other domains

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

your valid URL is: 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);
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消