Page 1 of 2

API call via Javascript

Posted: Thu Dec 04, 2014 8:52 pm
by nate7260789

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);


API call via Javascript

Posted: Thu Dec 04, 2014 9:36 pm
by Evgene Karachevtsev

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.


API call via Javascript

Posted: Thu Dec 04, 2014 9:42 pm
by nate7260789

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...".


API call via Javascript

Posted: Thu Dec 04, 2014 10:13 pm
by Evgene Karachevtsev

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.


API call via Javascript

Posted: Thu Dec 04, 2014 11:48 pm
by nate7260789

I did that as listed above. But it fails.


API call via Javascript

Posted: Thu Dec 04, 2014 11:48 pm
by nate7260789

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!!!");
}
});


API call via Javascript

Posted: Fri Dec 05, 2014 1:59 am
by Yurii Orishchuk

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.


API call via Javascript

Posted: Tue Dec 09, 2014 7:44 pm
by nate7260789

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


API call via Javascript

Posted: Wed Dec 10, 2014 12:59 am
by Yurii Orishchuk

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.


API call via Javascript

Posted: Wed Dec 17, 2014 4:16 pm
by nate7260789

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