views:

334

answers:

3

Hi,

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"&gt;&lt;/script&gt;
</head>
<body>
    <div id="images"></div>
<script>$.getJSON("http://tagthe.net/api/?url=http://www.knallgrau.at/en&amp;view=json&amp;callback=MyFunc",function(data){         
               alert(data);
        });
</script>
</body>
+2  A: 

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.

Matti Virkkunen
It's also possible to define a callback for the return JSON object. If the parameters callback and view=json are given the response is encapsulated in a function call to the given callback. So the above request would return MyFunc({"memes" ... }) if the parameter callback=MyFunc was present.This is the link of API :http://www.tagthe.net/fordevelopers
asilloo
@asilloo: Yes, and jQuery has built-in support for this (it's commonly known as "JSONP"). You don't have to define the callback by hand, since jQuery does it for you if you do what I said.
Matti Virkkunen
Martti: as you said I deleted "callback" and changed the code to $.getScript("http://tagthe.net/api/?url=http://www.knallgrau.at/enNow I don't have any error but in this case should "alert(data)" work or not because the window is empty. I want to get the data from tagthe.net
asilloo
@asilloo: what does ‘work’ mean? What should your code do? The window is empty because there's nothing in your HTML. Also, please read [How do comment replies work?](http://meta.stackoverflow.com/questions/43019/how-do-comment-replies-work)
Marcel Korpel
if everything goes ok, you should be getting an alert saying 'object'
GerManson
A: 

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"&gt;&lt;/script&gt;
</head>
<body>
    <div id="images"></div>
    <script>
        function MyFunc(data) {
            alert(data)
        }
        $.getScript("http://tagthe.net/api/?url=http://www.knallgrau.at/en&amp;view=json&amp;callback=MyFunc");
</script>
</body>
remi
Nope. jQuery takes care of this for you.
Matt
The new error;In Mozilla "undefined" .In I.E.8 Access denied line 5113 jquery-latest.js
asilloo
I updated my answer with some working code.
remi
Regarding your update: yes, that's because you provide a solution to the problem pointed out by Matti.
Marcel Korpel
Why would you define the callback yourself when jQuery can do it for you...
Matti Virkkunen
Oh well, I feel stupid now... I didn't read the question carefully.
remi
The new error; In Mozilla and I.E.8. they are saying [object Object]
asilloo
@asilloo: Geez, is "the new error" all you can say? That's not an error. That's your code working. Use Firebug and `console.log(data)` for a more useful view into your object.
Matti Virkkunen
@asilloo: First, you should update your original question or ask a new one, instead of commenting on someone's answer when you have another question. The message is correct, because the data you get “will be a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method.” See [jQuery.getJSON](http://api.jquery.com/jQuery.getJSON/)
Marcel Korpel
A: 

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&amp;view=json&amp;callback=MyFunc

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

$.getJSON("
    http://tagthe.net/api/url=http://www.knallgrau.at/en&amp;view=json&amp;callback=MyFunc",
    function(data){         
        alert(data);
    });
GerManson
1) JSONP is used to get around the same origin policy and 2) I believe the URL in the original is quite correct. At least it's not missing the "?" you say it is
Matti Virkkunen
the user edited the URL .... ...
GerManson
@Ger: where do you see that? http://stackoverflow.com/posts/2838150/revisions
Marcel Korpel