Retrieving REST Data
I'm sending data via ajax to my REST service, and while retrieving any information sent in the url parameter is easy when they're defined in the route, I can't get anything if I store information in the data parameter. For example:
$.ajax({
url: "ServerURL",
data: { "some": "json" } //How do I get this information?
...I've looked at many common solutions such as here:
https://community.intersystems.com/post/lets-write-angular-1x-app-cach%C3%A9-rest-backend-part-9
However, %request seems empty for the most part. I can get the url of the request and the method type, but Data, Content, Content.Read(), etc. are always empty. I feel like something simple is missing here that I'm not seeing. Any help is appreciated!
Thanks,
David
Comments
Hi David
Are you specifying the HTTP verb in the $.ajax call?
Thanks
Chris
Yes, this is the whole call, with the intention of receiving whatever I sent back to me:
$.ajax({
headers: { "Authorization": "Basic " + btoa("username:password") },
url:"serverurl/returnresponse",
type: "POST",
data: {
"payload": "some data"
},
success: function (response) {
console.log(response);
}
});
Then the route:
<Route Url="/returnresponse" Method="POST" Call="ReturnResponse" Cors="false"/>
The method:
ClassMethod ReturnResponse() as %Status {
try {
set obj = %request.Data //...Contents...etc.
write obj
}
catch ex {
write ex.Name
}
return $$$OK
}
Fantastic! This is perfect, thank you so much!
From your example, at a minimum I had to add two parameters to my ajax:
contentType: "application/json",
dataType: "json",
And then to retrieve them, the REST service simply needs this to send back to the client whatever was in the message:
set jsonstring = $ZCONVERT(%request.Content.Read(),"I","UTF8")
set data = {}.%FromJSON(jsonstring)
write data.%ToJSON()
Thanks again!
David
Try reading the Request data with this
Set updateJSON = %request.Content.Read()
Edited because I missed that you already tried this?
Right, I get an <INVALID OREF> error when I try that.
Could you try making the call with a REST client like RESTLet for Chrome, and seeing if the request comes through on that method?
The message gets sent and a reply comes through successfully, but the body information still isn't seen by the rest service.
Take a look to this simple example:
https://github.com/drechema/isc-coffe-shop
Maybe it helps to detect the problem