javascript - Using POST and GET to Exchange Data Through Endpoints -
i'm attempting build web app using spotify's web api however, find tutorial lacking. way works data requested using post , functionality , sent using json. seems easy enough, want information artist? call https://api.spotify.com/v1/artists/0oduwj0sbjdrqhygguxecf
, nice json document information requested artist.
the problem don't know how make call. jquery has , post methods both "data" parameters i'm not sure on syntax necessary, when comes exchanging authorization code access token. if visit spotify's authorization guide , scroll step 4 of authorization code flow can see need make post call https://accounts.spotify.com/api/token
. call must have 3 request body parameters , 1 header parameter , upon succession json file appropriate data in response body.
my question how make post , calls have body parameters , header parameters , how extract json data response body after successful call?
as can see code examples & libraries , jsfiddle, getuserdata request nothing simple ajax call, contains url , headers object (which contains prefix string concatenated accesstoken) parameters:
function getuserdata(accesstoken) { return $.ajax({ url: 'https://api.spotify.com/v1/me', headers: { 'authorization': 'bearer ' + accesstoken } }); }
generally, when need pass parameters in $.ajax
call, shown above, or construct object first , include this:
yourobj = { url: "your url here", param2: "param val 2", param3: "param val 3", ... } $.ajax(yourobj).done(function(data){ //do returned data here, e.g. console.log("data: ", data); });
this approach can useful if parameters dependent on other values not readily avalable.
Comments
Post a Comment