0

Ajax 调用的新手。需要一些指导来开始我的学习,但想用一个真实的例子来做。

此链接:http ://api.tumblr.com/v2/blog/david.tumblr.com/info?api_key=API_KEY

将此返回到我的浏览器,这是我将使用的数据的一个很好的示例:

{
    "meta": {
        "status": 200,
        "msg": "OK"
    },
    "response": {
        "blog": {
            "title": "David\u2019s Log",
            "posts": 3981,
            "name": "david",
            "url": "http:\/\/www.davidslog.com\/",
            "updated": 1348785660,
            "description": "\u201cMr. Karp is tall and skinny, with unflinching blue eyes and a mop of brown hair. He speaks incredibly fast and in complete paragraphs.\u201d \u2013 NY Observer",
            "ask": false
        }
    }
}

我想我可以以此为起点,但绝对可以接受其他方式:

var tumblrURL = "david.tumblr.com";

jQuery.ajax({
    url: "http://api.tumblr.com/v2/blog/" + tumblrURL + "/info",
    data: {api_key: "MY_PRIVATE_API_KEY"},
    dataType: "jsonp",
});​

但是我不知道正确的方法来解析并返回不同的位并将它们用作变量或用于调用中的测试。

例如,如何获取“状态”或“名称”?

当状态为 200 时,我想用名称(“david”)创建一个 var 并将其用于其他选项。

或者,如果状态是 404,那么我想做其他事情(比如返回错误)。

Please, no flames. I'm trying to learn and if I get a few examples or pointers I will be able to move on to more interesting stuff. I'm OK with other aspects of jQuery so those issues should be fine.

Thanks!

4

2 回答 2

1

The jQuery.Ajax function takes an optional function parameter success:

success(data, textStatus, jqXHR)

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

You could set this to your own function, which would automatically execute once the request succeeds - providing you with the entire JSON blob you receive as a normal Javascript object. In your case, you could get the status of the request at data.meta.status - or the name at data.response.blog.name.

Note that jQuery also allows for error handlers on Ajax requests, called automatically in a similar way: error(jqXHR, textStatus, errorThrown).


Example code:

jQuery.ajax({
    url: "http://api.tumblr.com/v2/blog/" + tumblrURL + "/info",
    data: {api_key: "MY_PRIVATE_API_KEY"},
    dataType: "jsonp",
    success: function(data, textStatus, jqXHR) {
        // Do whatever you'd like in here with your data
        console.log("Got a response from " + data.response.blog.name);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        // Do whatever you'd like in here with your ERROR data
        alert("Got an error: " + textStatus);
    }
});​
于 2012-09-29T02:43:16.523 回答
1

Generally after a ajax call happens , if the request is a success i.e, if you get the response you access it in the success callback function...

jQuery.ajax({
    url: "http://api.tumblr.com/v2/blog/" + tumblrURL + "/info?api_key=MY_PRIVATE_API_KEY",  // The url you want to pass
    dataType: "jsonp",  // The type of format expecting
    data: "{}",  // Your data to be sent
    success: function(result) {   // If ajax is a success
       alert('Ajax request Success !!');
   },
    error: error(jqXHR, textStatus, errorThrown) {  //If error in ajax
        alert('Error occured  : ' + errorThrown);
    }
});

This is just the basic setup of ajax request.. You have many more options whih you can look up in jQuery Documentation .

The jsonp data you get in the callback look's like the one in the example..

Here result is a object .. You generally use . notation or [] notation to access the result.

To get the data inside meta you use result.meta.status To get the data inside name you use result.response.blog.name

You can always use console.log( result.meta.status) to log the value onto the console.

于 2012-09-29T03:02:46.183 回答