Page 1 of 1

RESTful Web Services return value

PostPosted: Sat Mar 05, 2016 8:19 pm
by marco.rossi
Hi all,


I'm getting error with RESTful by returning an object with more than 1 property.

ex:

Code: Select all
var _obj = new Object()
     _obj.id = 1
return _obj


works fine, into the response body I get an xml with a key "ID" and a value "1".

Code: Select all
var _obj = new Object()
     _obj.id = 1
     _obj.myName = "Marco"
return _obj


Gives the error shown within the picture attached.
error.png
error.png (12.56 KiB) Viewed 3874 times


Am I missing something?

Regards

Marco

Re: RESTful Web Services return value

PostPosted: Mon Mar 07, 2016 12:21 am
by Ruben79
Your browser is complaining because the result isn't valid xml, because there is no root element.
The xml will be something like <id>1</id><myName>Marco</myName> if you check the page source.

To have a valid xml, wrap your result in a single node, like this:
Code: Select all
var _obj = new Object()
_obj.result = new Object()
_obj.result.id = 1
_obj.result.myName = "Marco"
return _obj

Another tip is to use a more declaritive approach using this literal notation as example:

Code: Select all
var id = 1;
var myName = 'Marco';

var result = {
   result: {
      id: id, myName: myName
   }
}
   
return result;