简体   繁体   中英

How to catch the response in ajax call?

</html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$.ajax({
            type: 'GET',
            url: 'http://api.twitter.com/1/statuses/retweeted_to_me.json?&callback=?',
            success: function(data) {
        alert(data);
        },
        error: function(data) {

        }
        });

</script>
</html>

I know that this urls gives me when I hit this url on browser.:

{
error: "Could not authenticate you."
request: "/1/statuses/retweeted_to_me.json"
}

I want to catch the same thing on my ajax call above. Now alert(data) is empty

You need to add dataType: 'json' to your ajax-call. Then you can use:

alert(data.error);

Or, if it ends up in the error function:

error: function(req, err) {
   alert(req.responseText);
}

Move your alert(data); to the error: handling function and it should show since you are recieving an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM