Page 1 of 1

call external rest service in javascript(not in server code)

Posted: Sun Jun 22, 2014 6:10 am
by Zhong Liu

Hello,
Can I call third party rest service in my javascript? I tried to wrote a piece of code like this:
Appery.testCallRest = Appery.createClass(null, {

Code: Select all

 init: function(requestOptions) { 
     this.__requestOptions = $.extend({}, requestOptions); 
 }, 

 process: function(settings) { 

     if (this.__requestOptions.echo) { 
         settings.success(this.__requestOptions.echo); 
     } else { 
         var xmlhttp = new XMLHttpRequest(); 
         xmlhttp.onreadystatechange=function(){ 
 if (xmlhttp.readyState==4) 
             {// 4 = "loaded" 
                 if (xmlhttp.status==200) 
                 {// 200 = OK 
                     // ...our code here... 
                     settings.success(XHRResponse); 
                     settings.complete('success'); 
                 } 
                 else 
                 { 
                    alert("Problem retrieving XML data"); 
                 } 
             } 
         }; 
         xmlhttp.open("post","[url=http://mydomain.com/rest/service_name",true]http://mydomain.com/rest/service_name...[/url]); 
         xmlhttp.send(null); 
     } 
 } 

});

when I define a service using this code, at run-time system report error:
XMLHttpRequest cannot load http://mydomain.com/rest/service_name. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://appery.io' is therefore not allowed access.

I knew I should define a rest service and point the url to external rest url, however the outside service need me to give a multipart-form data, I don't know how to give that data when defining rest service in appery.io.


call external rest service in javascript(not in server code)

Posted: Sun Jun 22, 2014 6:41 am
by Alena Prykhodko

Hello,

Sure, you can use 3rd party APIs via JS (you can look through some tutorials http://devcenter.appery.io/tutorials/...)

Please try with Use Appery.io Proxy on http://devcenter.appery.io/documentat....


call external rest service in javascript(not in server code)

Posted: Sun Jun 22, 2014 7:04 am
by Zhong Liu

Hi Alena,
If my 3rd party service need a multipart-form data, how to define the rest service in appery.io, do you have an example to me to follow, thank you.


call external rest service in javascript(not in server code)

Posted: Sun Jun 22, 2014 11:04 am
by Zhong Liu

what I mean is in appery.io, how can I pass a multipart-form data to the service?


call external rest service in javascript(not in server code)

Posted: Mon Jun 23, 2014 2:31 am
by Yurii Orishchuk

Hi Shong.

You need:

1 Create REST service.

2 Open Settings tab and set following fields:

Code: Select all

 2.1 URL - your external service URL. 

 2.2 Method - "POST" 

 2.3 Content Type - "x-www-form-urlencoded" 

 2.4 Use Proxy - you need to select proxy and check checkbox. 

Regards.


call external rest service in javascript(not in server code)

Posted: Mon Jun 23, 2014 2:56 am
by Zhong Liu

Thank you, Yurii.
then, when I call the rest service, how do I give the multipart-form parameter when I do the test on defining rest service GUI, could you please give me an example.


call external rest service in javascript(not in server code)

Posted: Mon Jun 23, 2014 3:56 pm
by Zhong Liu

Hi Yurii,
I defined a rest service, and add a datasource on the page like this:

Image

then I put a piece of js code on 'click' event of a button like this:

Image

when I test the app by launch and click the button, the rest service can be called and run successfully, the request parameter can be passed to the rest service.

However, when I define a rest service which is used to upload an image file, the server need a multipart-form data like this:

Image

I don't know how to pass this kind of data to service when calling service_name.execute(); could you please give me your suggestion, thank you!


call external rest service in javascript(not in server code)

Posted: Mon Jun 23, 2014 6:03 pm
by Evgene Karachevtsev

Hello Zhong ,

You can't use the standard REST service.
You should use ajax request directly (in a function or in a generic service)
Here is an example:
http://stackoverflow.com/a/5976031


call external rest service in javascript(not in server code)

Posted: Mon Jun 23, 2014 11:10 pm
by Zhong Liu

Hi Evgene,
I tried the ajax, system report cross domain call error, how can I set proxy when direct call from JS?


call external rest service in javascript(not in server code)

Posted: Tue Jun 24, 2014 6:50 am
by Kateryna Grynko

Hi Zhong,

You can read about Generic service here: http://devcenter.appery.io/documentat...

You could enable proxy for it and also custom service realization.