Zhong Liu
Posts: 0
Joined: Tue Jun 17, 2014 3:27 am

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

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.

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

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

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

Zhong Liu
Posts: 0
Joined: Tue Jun 17, 2014 3:27 am

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

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.

Zhong Liu
Posts: 0
Joined: Tue Jun 17, 2014 3:27 am

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

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

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

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

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.

Zhong Liu
Posts: 0
Joined: Tue Jun 17, 2014 3:27 am

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

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.

Zhong Liu
Posts: 0
Joined: Tue Jun 17, 2014 3:27 am

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

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!

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

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

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

Zhong Liu
Posts: 0
Joined: Tue Jun 17, 2014 3:27 am

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

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

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.

Return to “Issues”