绝对基础--JSON和jQuery $ ajax

时间:2014-06-14 22:59:47

标签: javascript jquery ajax json

我第一次尝试使用JSON并使用GET从外部网站提取数据。

此时我想要的是将数据从ip.jsontest.com提取到我的JavaScript中,然后提醒我的IP地址。

我尝试按照api.jquery.com/jquery.getjson/

进行布局
$.ajax({
  dataType: "json",
//how do I know what my dataType is?
  url: url,
//Ok, I get this one http://ip.jsontest.com/
  data: data,
/*Don't know what to put in here either, the URL should return "{"ip": "8.8.8.8"}" 
but I don't know what that means for this line.
*/
  success: success
/*I get that this is what to do with the success, but all
I want to do is have the single item object available 
to use in my .js so I am not sure if I am supposed to put 
in a function here and assign it to a var or what.
*/    
});

因为我不明白每条线的含义是什么...... 我一直在尝试这样的事情:

$.ajax({
    type: "GET",
    dataType: "json",
    url: "http://ip.jsontest.com/",
    data: {key:key},
    success: function (data) {
    var ip1 = data.ip;
    }
});

但是把它放在我的.js中打破了整个事情...所以我希望有人能告诉我使用该信息的正确方法,然后提醒返回的IP地址......

1 个答案:

答案 0 :(得分:0)

数据类型 - 您希望从服务器返回的数据类型。既然你说你试图使用json我会认为它是json 数据 - 是您发布到服务器的数据 - 因为您没有发布任何您不需要的数据。
成功 - 如果您的ajax呼叫成功,您希望发生什么。

试试这个:

success: function(response){
                alert(response);
            }