· Feb 15, 2018

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:

               url: "ServerURL",
               data: { "some": "json" } //How do I get this information?


I've looked at many common solutions such as here:

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!



Discussion (8)2
Log in or sign up to continue

Yes, this is the whole call, with the intention of receiving whatever I sent back to me:

                headers: { "Authorization": "Basic " + btoa("username:password") },
                type: "POST",
                data: {
                    "payload": "some data"
                success: function (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!