无法完成.getJSON请求? [英] Unable to complete the .getJSON request?
问题描述
请参阅下面的代码。如果无法用jQuery执行请求,那么您能否给我一个另一种方法来做到这一点? (例如AJAX)我已经完成了我的研究,但是我承认我不了解足够的理解为什么请求不起作用。我需要在显示警报之前插入一个等待响应的参数吗?还是隐含的?提前致谢!
< html lang =en>
< head>
< meta charset =utf-8>
< title> jQuery demo< / title>
< / head>
< body>
< a href =http://jquery.com/> jQuery< / a>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js>< / script>
< script>
$(document).ready(function(){
$(a)。click(function(event){
$ .getJSON('http://www.strava。 com / api / v1 / segments / 637215',函数(data){
alert(data);
});
});
});
< / script>
< / body>
< / html>
有关您的解决方案为何无法运作的更多信息按照预期,请参阅:访问控制 - 允许来源多个来源域?
尝试使用 .ajax
方法替代 dataType:jsonp
:
$。ajax({
url:http:// www .strava.com / api / v1 / segments / 637215,
dataType:'jsonp',
成功:函数(数据){
console.log(data);
}
});
运行此操作会得到以下结果:
{segment:{id:637215,name:Highway 33 Climb - Matilija Lake to Rose Valley,distance:16779.2,elevationGain:1101.1, elevationHigh:1070.9,elevationLow:289.54,averageGrade:4.64087,climbCategory:1}}
请注意,返回的数据似乎存在错误,但我能够看到它。 (请参阅下面的 Musa 的评论)。
编辑
或者您可以更改:
$ .getJSON('http://www.strava.com/api / v1 / segments / 637215',函数(data){
console.log(data);
});
到:
$ .getJSON('http://www.strava.com/api/v1/segments/637215?callback=?',function(data){
console.log(data);
});
这会导致 .getJSON()
到改用 jsonp
。
如果URL包含字符串callback =? (或类似的,由服务器端API定义),请求将被视为JSONP。
To start off, this is a cross-domain request I am trying to complete. Here is a link to the Strava API whitepaper that I am using as a reference. Strava Wiki
Please see the code I am using below. If it is not possible to perform the request with jQuery, can you give me an example of another way to do it? (ex. AJAX) I've done my research but I admit that I do not know enough to understand why the request is not working. Do I need to insert an argument that waits for the response before displaying the alert, or is that implied? Thanks in advance!
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery demo</title>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("a").click(function(event){
$.getJSON('http://www.strava.com/api/v1/segments/637215', function(data) {
alert(data);
});
});
});
</script>
</body>
</html>
For more information as to why your solution will not work as intended, please refer to: Access-Control-Allow-Origin Multiple Origin Domains?
Try using the .ajax
method instead with dataType: "jsonp"
:
$.ajax({
url: "http://www.strava.com/api/v1/segments/637215",
dataType: 'jsonp',
success: function(data){
console.log(data);
}
});
running this gets the following:
{"segment":{"id":637215,"name":"Highway 33 Climb - Matilija Lake to Rose Valley","distance":16779.2,"elevationGain":1101.1,"elevationHigh":1070.9,"elevationLow":289.54,"averageGrade":4.64087,"climbCategory":"1"}}
Note that there does seem to be an error with the returned data, but I am able to see it. (Please see Musa's comment below).
EDIT
or you can change:
$.getJSON('http://www.strava.com/api/v1/segments/637215', function(data) {
console.log(data);
});
to:
$.getJSON('http://www.strava.com/api/v1/segments/637215?callback=?', function(data) {
console.log(data);
});
This will cause .getJSON()
to use jsonp
instead.
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead.
这篇关于无法完成.getJSON请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!