Question

I'm using IBM Worklight for my mobile app project. My problem is, how to stringify JSON in worklight adapter?

Username-impl.js

 function getUsername(userAlias) {
    path = "rest-rib/service/Login/login_username?userAlias=" + userAlias + "&locale=en";

    var input = {
        method : 'post',
        returnedContentType : 'json',
        path : path
    };


    return WL.Server.invokeHttp(input);
}

I got this error when invoke the adapter.

{
   "errors": [
      "Runtime: Failed to parse JSON string\nError 415: Unsupported Media Type"
   ],
   "info": [
   ],
   "isSuccessful": false,
   "warnings": [
   ]
}

Thanks a lot in advance.

Was it helpful?

Solution

I got the answer

function getUsername(userAlias) {
        WL.Logger.debug("Entering ContactRESTService1.getUsername()");
        path = '/rest-rib/service/Login/login_username';

         var input = {
                      method : 'post',
                      returnedContentType : 'json',
                      path : path,
                      body:{
                      contentType:'application/json; charset=UTF-8',
                      content:
                          JSON.stringify({
                              "userAlias":userAlias,
                              "locale":"en"
                          })
                      }
         };
         WL.Logger.debug("Exiting ContactRESTService1.insertContact()");

         return WL.Server.invokeHttp(input);
}

OTHER TIPS

var jsonString = JSON.stringify(jsonObj);

other way around

var jsonObj = JSON.parse(jsonString);

Note Idan's answer above. If you're talking about parsing response from a backend - WL does this for you automatically.

Do you mean how to stringify the response? Worklight does this for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top