使用JSONP从另一台服务器获取JSON数据 [英] Using JSONP to get JSON data from another server

查看:116
本文介绍了使用JSONP从另一台服务器获取JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一家公司托管的网站收集JSON数据。我不在同一个域中,所以我不能访问它。从标题看来,它们似乎也不使用CORS。所以我试图使用JSONP来解析数据,但它似乎不起作用。我试过做$ .getJSON和$ .ajax,但那些抛出错误,不要调用函数。这是我的解决方案,在服务器的响应中,它仍然不会将数据包装在getResults中。

  <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN> 
< script type =text / javascriptsrc =jquery / js / jquery-1.9.0.min.js>< / script>

< script>
function getResults(data){
console.log(data [0] .name);
}
< / script>
< script type ='text / javascript'src ='https://solstice.applauncher.com/external/contacts.json?callback = getResults'>< / script>









我对HTML,JavaScript和jQuery相当陌生,所以我只是很困惑为什么响应被封装在函数和控制台日志中没有工作。任何帮助,将不胜感激。

解决方案

您可以使用多种方法,但最方便的两种方法是AJAX调用或使用jQuery的getJSON()方法



使用AJAX调用
$ b

  $(文件).ready(function(){
$(。btn)。click(function(){
$ .ajax({type:get,
url:http ://api.forismatic.com/api/1.0/,
data:{method:getQuote,格式:jsonp,lang:en},
dataType:jsonp ,
jsonp:jsonp,
jsonpCallback:myJsonMethod
});
});
});
函数myJsonMethod(响应){
console.log(response);

$ / code>

在上面的方法 response Object 具有来自API调用的所有JSON数据。



使用getJSON()


$ b $ (函数()){
$ .getJSON()函数http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&lang=en&jsonp=myJsonMethod&?callback=?);
});
});
函数myJsonMethod(响应){
console.log(response);

$ / code>

在上面的方法中,同样的事情发生。 b

注 - > JSONP取回响应函数的名称。

I'm trying to collect JSON data from a website hosted by a company. I am not on the same domain, so I can't just access it. From the headers, it seems that they don't use CORS either. So I've tried to use JSONP to parse the data, but it doesn't seem to work. I've tried doing $.getJSON and $.ajax, but those throw errors and don't call the function. This is my solution thus far, and in the response from the server, it still doesn't wrap the data in getResults.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<script type="text/javascript" src="jquery/js/jquery-1.9.0.min.js"></script>

<script>
function getResults(data) {
    console.log(data[0].name);
}
</script>
<script type='text/javascript' src='https://solstice.applauncher.com/external/contacts.json?callback=getResults'></script>

I'm fairly new to HTML, JavaScript, and jQuery, so I'm just really confused why the response is being wrapped in the function and console log isn't working. Any help would be appreciated.

解决方案

You can use many ways but the two most convenient ones are either an AJAX call or to use jQuery's getJSON() method

Using AJAX call

$(document).ready(function() {
  $(".btn").click(function() {
    $.ajax({type: "get",
            url: "http://api.forismatic.com/api/1.0/",
            data: {method: "getQuote",format: "jsonp",lang: "en"},
            dataType: "jsonp",
            jsonp: "jsonp",
            jsonpCallback: "myJsonMethod"
    }); 
  });
});
function myJsonMethod(response){
  console.log (response);
}

In the above method response Object has all the JSON data from the API call.

Using getJSON()

$(document).ready(function() {
  $(".btn").click(function() {
    $.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&lang=en&jsonp=myJsonMethod&?callback=?");
  });
});
function myJsonMethod(response){
  console.log (response);
}

In the above method the same thing happens.

NOTE --> That JSONP takes the name of the callback function on which the response is to be sent.

这篇关于使用JSONP从另一台服务器获取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆