nate7260789
Posts: 0
Joined: Thu Dec 04, 2014 8:41 pm

API call via Javascript

I have an API that I need to call from JavaScript. The error I am getting is No 'Access-Control-Allow-Origin' . I think that this may be because of the need for the proxy tunneling routine.
So what I am trying to to do is pass a song id that is returned by one API and mapped to a page and then find the corresponding song name using this Javascript API call.

here is the javascipt ---(function(context) {
Song_Helper = {};

Code: Select all

 Song_Helper.getSongInfo = function(songid) { 

     var result = []; 

     $.ajax('[url=http://dev.touchtunes.com/music/song?song_id]http://dev.touchtunes.com/music/song?...[/url]=' + songid + '&api_key=ulzuxxxxxxmykey', { 
         type: 'GET', 
         dataType: 'json', 
          beforeSend: function(xhr) { 
              xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); 

          }, 
         success: function(data) { 

             result = data; 
         }, 
         complete: function(resp) { 

             console.log(resp); 
         }, 
         error: function(jqXHR, textStatus, errorThrown) { 

         } 
     }); 

     return result; 
 }; 
 context.Song_Helper = Song_Helper; 

})(window);

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

API call via Javascript

Hello Nate,

Yes, you need to use proxy. Our advice is to change you javascript call to Appery.io REST services and then you can easily make connection via proxy.

nate7260789
Posts: 0
Joined: Thu Dec 04, 2014 8:41 pm

API call via Javascript

Can you provide an example? I tried calling the appery REST service also but it fails.
TTMusicSearchSongID.execute({

Code: Select all

         where: { 
             "song_id": songid 
         }, 
         success: function(data) { 
             alert("success!!!" + data.length); 
         }, 
         //complete: function(resp) { 

         //  alert(JSON.stringify(resp)); 
         //}, 
         error: function(jqXHR, textStatus, errorThrown) { 
             alert("error!!!"); 
         } 
     }); 

Refused to set unsafe header "Accept-Encoding"
XHR finished loading: GET "https://api.appery.io/rest/1/proxy/tu...".

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

API call via Javascript

Nate,

It should be easy in javascript
service_name_onthis_page.execute({}) - // this means it will invoke it with default params.

You can invoke service on page show or load event. It is better to process Success and Complete events in Data tab of the current page.

nate7260789
Posts: 0
Joined: Thu Dec 04, 2014 8:41 pm

API call via Javascript

I did that as listed above. But it fails.

nate7260789
Posts: 0
Joined: Thu Dec 04, 2014 8:41 pm

API call via Javascript

This is what I did. see below:

TTMusicSearchSongID.execute({

where: {
"song_id": songid
},
success: function(data) {
alert("success!!!" + data.length);
},
//complete: function(resp) {

// alert(JSON.stringify(resp));
//},
error: function(jqXHR, textStatus, errorThrown) {
alert("error!!!");
}
});

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

API call via Javascript

Hi Nate,

You forgive to wrap request parameter into "parameters" object.

Here is code:

pre

TTMusicSearchSongID.execute({
parameters: {
where: {
"song_id": songid
}
},
success: function(data) {
alert("success!!!" + data.length);
},
//complete: function(resp) {

Code: Select all

 // alert(JSON.stringify(resp));  
 //},  
 error: function(jqXHR, textStatus, errorThrown) { 
     alert("error!!!"); 
 } 

});

/pre

Regards.

nate7260789
Posts: 0
Joined: Thu Dec 04, 2014 8:41 pm

API call via Javascript

Hi Yurii, I made the change as defined. I called the JavaScript function called getSong() from the mapping routine. So this javascript is called after success and mapped to a field called songname. But it still does not work. The code enters the function successfully but it does not seem to run the Appery execute() function.

Any idea how to fix? Thanks

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

API call via Javascript

Hi Nate,

Sorry "where" parameters should be passed as string of json.

Code:

precode

var whereObject = {
"song_id": songid
};

TTMusicSearchSongID.execute({
parameters: {
where: JSON.stingify(whereObject)
},
success: function(data) {
alert("success!!!" + data.length);
},
//complete: function(resp) {
// alert(JSON.stringify(resp));
//},
error: function(jqXHR, textStatus, errorThrown) {
alert("error!!!");
}
});

/code/pre

Regards.

nate7260789
Posts: 0
Joined: Thu Dec 04, 2014 8:41 pm

API call via Javascript

Hi, the parameter call does not work but when I set
data:{song_id: 86091202, api_key: myapikeyyyyy} instead, I get a success.
The problem is that the success returns null. The process uses a proxy to make the call. As you can see below, the proper parameters are being passed as expected. Any idea why I get no results? I can run the service call directly and get results but not via a javascript call. Thanks
Request URL: https://api.appery.io/rest/1/proxy/tu...
Request Method: GET
Status Code: HTTP/1.1 200 OK

Return to “Issues”