Why won't this jQuery ajax call work in chrome

I cannot get this $.ajax function to work in Chrome. When I make the ajax request, a response is never returned. When I view this in Chrome dev tools, it states that the json request is still pending. So far, I have found suggestions to add these parameters to the options.

'{type: "post", data: '', cache: false, async: false}'

However, none of these options made it work.

        try {
            var o = {username: 'adobeedge', count: 4};
            var twitterUrl = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' + o.username + '&count=' + o.count;

            $("body").append('<p>attempting ajax</p>');

            $.ajax({url: twitterUrl,
                dataType: 'jsonp',
                type: "post",
                data: '',
                cache: false,
                async: false,
            })
                    .success(function(data) {

                    $.each(data, function(index, item) {
                        $("body").append(item.text);
                    });
                }) ;
        }
        catch (error) {
            alert("Error: " + error.toString());
        }

Following code works fine in my case

My chrome Version : 23.0.1271.95 m

try {
        var o = {username: 'adobeedge', count: 4};
        var twitterUrl = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' + o.username + '&count=' + o.count;

        $("body").append('<p>attempting ajax</p>');

        $.ajax({url: twitterUrl,
            dataType: 'jsonp',
            type: "post",
            data: '',
            cache: false,
            async: false,
        })
                .success(function(data) {

                $.each(data, function(index, item) {
                    $("body").append(item.text);
                });
            }) ;
    }
    catch (error) {
        alert("Error: " + error.toString());
    }

I don't know if this will be of any help, but I've never seen the success function attached on the outside of the ajax call. All the jQuery ajax documentation I've looked at shows the ".done" attached to the end. However, I've only seen success defined as an option inside the actual ajax call itself. You could try rewriting it as follows:

$.ajax({
    url: twitterUrl,
    dataType: 'jsonp',
    type: "post",
    data: '',
    cache: false,
    async: false,
    success: function(data) {
        $.each(data, function(index, item) {
            $("body").append(item.text);
        });
    }
});

See if this helps. There is also another Stack Overflow Article that may be of some help to you as well. This article describes the different uses of "success:" vs. ".done". For more information on the .ajax documentation visit the jQuery API documentation for .ajax.

Hope this helps.


jsonp does not support synchronous calls. Try async: true .

I'm not able to try it as I work in an office where twitter is blocked.

链接地址: http://www.djcxy.com/p/96480.html

上一篇: $ .ajax成功回调不在firefox中触发

下一篇: 为什么这个jQuery ajax调用不能在chrome中工作