MobileFirst Platform JavaScript adapter can't get the parameters through WLResourceRequest

老子叫甜甜 提交于 2019-12-14 03:48:41

问题


I'm using mobilefirst platform v7, and I send post request using the WLResourceRequest/sendFormParameters api, however, I can't get the submitted parameters from js adapter side...

belows are sample code:

var resourceRequest = new WLResourceRequest("adapters/businessAdapter/flightsearch", WLResourceRequest.POST);
var params={
        "flightNum":'mu8899',
        "departCity":'SHA',
        "destCity" :'PEK'
};
resourceRequest.sendFormParameters(params).then(
        callSuccess,
        callFailure
);

js adapter code:

function flightsearch(params) {
   WL.Logger.info("get params "+params);


    var input = {
        method : 'post',
        returnedContentType : 'json',
        path : 'restapi/api/flightsearch',
        body :{
            contentType: 'application/json; charset=utf-8',
            content:params
        },
        headers: {"Accept":"application\/json"} 
    };

    return WL.Server.invokeHttp(input);
}

回答1:


The syntax you used is fine for Java adapters.

However, in the case of JavaScript adapters, procedure parameters are handled differently.

First, your adapter procedure should define the parameters that it expects:

function flightsearch(flightNum, departCity, destCity) {
///
}

Secondly, this procedure will be triggered using an HTTP GET or POST with a single parameter called params which needs to contain an array, representing all the procedure parameters in the correct order:

params:["mu8899","SHA","PEK"]

Now using JavaScript, this would translate to:

var resourceRequest = new WLResourceRequest("adapters/businessAdapter/flightsearch", WLResourceRequest.POST);
var params=[
        'mu8899',
        'SHA',
        'PEK'
];
var newParams = {'params' : JSON.stringify(params)};
resourceRequest.sendFormParameters(newParams).then(
        callSuccess,
        callFailure
);

As you can see, we first build the JSON array (note, array not object) in the correct order, then we convert it to String and send it to the adapter with the parameter name 'params'.



来源:https://stackoverflow.com/questions/30044473/mobilefirst-platform-javascript-adapter-cant-get-the-parameters-through-wlresou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!