本文介绍了MobileFirst Platform JavaScript适配器无法通过WLResourceRequest获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mobilefirst平台v7,我使用WLResourceRequest / sendFormParameters api发送post请求,但是,我无法从js适配器端获取提交的参数...

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...

以下是示例代码:

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

js适配器代码:

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


推荐答案

你使用的语法很好对于Java适配器。

The syntax you used is fine for Java adapters.

但是,对于JavaScript适配器,过程参数的处理方式不同。

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) {
///
}

其次,此过程将使用HTTP <$ c触发$ c> GET 或 POST ,带有一个名为 params 的参数,需要包含一个数组,以正确的顺序表示所有过程参数:

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"]

现在使用JavaScript,这将转换为:

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

如您所见,我们首先构建JSON数组(注意,数组 not object)按正确的顺序,然后我们将它转​​换为String并将其发送到参数名为' params '的适配器。

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'.

这篇关于MobileFirst Platform JavaScript适配器无法通过WLResourceRequest获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:13
查看更多