简体   繁体   中英

IBM Worklight 6.1 - How to stringify JSON in worklight adapter?

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.

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM