I am trying to get data using the strava v3 api. I am very new to JS. I copied the example XMLHttpRequest code, but all I get is an empty string returned. If I go to the link manually in the browser, it works fine. I've also tried this using jquery and I get the same thing.
function reqListener() {
console.log(this.responseText);
};
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "https://www.strava.com/api/v3/athlete/activities?per_page=1&access_token=83ebeabdec09f6670863766f792ead24d61fe3f9", true);
oReq.send();
Any ideas?
Edit: with jquery. Still doesn't work
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<span id="hello">hello</span>
<script>
$(document).ready(function() {
$.getJSON("https://www.strava.com/api/v3/athlete/activities?per_page=1&access_token=83ebeabdec09f6670863766f792ead24d61fe3f9&callback=?", function(data) {
$('#hello').html(data);
});
});
</script>
</body>
</html>
From their documentation:
JSON-P callbacks
Specify the ?callback=function_name parameter to request a JSONP compatible response. This is compatible with JavaScript libraries such as jQuery.
So I'd suggest you use jQuery to trigger a JSONP request to them:
$.getJSON( "https://www.strava.com/api/v3/athlete/activities?per_page=1&access_token=yourAccessToken&callback=?", function (data) {
// your code in here
});
As per comment below: The callback=? in this form (no replacement of the ?) signals to jQuery, that this is a JSONP request. jQuery itself will change the ? to some function name for the callback, which basically executes the function you pass to $.getJSON().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With