0

This is my code:

$("document").ready(function () {
    var jqxhr = $.ajax({
        url: "http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
        success: function () {
            $("#foo").text("second success");
        },
        error: function (xhr, status, error) {
            $("#foo").text(JSON.stringify(xhr));
        },
        complete: function () {

        }
    });
    });

This is the markup:

<body>
    <div id="foo"></div>
</body>

The data that is returned (serialized and placed into #foo) is:

{
"readyState":0,
"responseText":"",
"status":0,
"statusText":"error"
}

I have no clue what I'm doing wrong. Please help.

Here is a fiddle: http://jsfiddle.net/U2gm9/4/

3
  • I just went to that URL and got no errors. Is flickr.com blocked where you are? What happens if you enter that URL into a web browser? Commented Nov 11, 2013 at 4:21
  • Please read the documentation carefully. The error callback does not receive a single data argument. Commented Nov 11, 2013 at 4:22
  • @Phil--I know that, I put it in just for convenience's sake. On a real call I would add the appropriate arguments. Commented Nov 11, 2013 at 4:25

1 Answer 1

3

Try setting dataType: 'jsonp'

$("document").ready(function () {
    var jqxhr = $.ajax({
        url: "http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
        dataType:'jsonp',
        success: function () {
            $("#foo").text("second success");
        },
        error: function () {
            $("#foo").text("error");
        },
        complete: function () {

        }
    })
});

Demo: Fiddle

Using $.getJSON()

$("document").ready(function () {
    $.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?').done(function (data) {
        $("#foo").text("second success");
        console.log(data)
    }).fail(function () {
        $("#foo").text("error");
    }).always(function () {})
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

what do I do if the server I am sending the request to does not support JSONP?
@codeninja since you are making a cross domain ajax request the target resource has to support jsonp or if you want to support only modern browsers then you can look at CORS
It doesn't seem like CORS requires me to add anything to my ajax call... or am I missing something? The example in the link you sent resembles what I have above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.