Phi*_*hao 3 ibm-mobilefirst mobilefirst-adapters
我正在使用mobilefirst平台v7,我使用WLResourceRequest/sendFormParameters api发送post请求,但是,我无法从js适配器端获取提交的参数...
以下是示例代码:
var resourceRequest = new WLResourceRequest("adapters/businessAdapter/flightsearch", WLResourceRequest.POST);
var params={
"flightNum":'mu8899',
"departCity":'SHA',
"destCity" :'PEK'
};
resourceRequest.sendFormParameters(params).then(
callSuccess,
callFailure
);
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
您使用的语法适用于Java适配器.
但是,对于JavaScript适配器,过程参数的处理方式不同.
首先,您的适配器过程应该定义它期望的参数:
function flightsearch(flightNum, departCity, destCity) {
///
}
Run Code Online (Sandbox Code Playgroud)
其次,此过程将使用HTTP GET或POST单个参数触发,该参数params需要包含一个数组,以正确的顺序表示所有过程参数:
params:["mu8899","SHA","PEK"]
Run Code Online (Sandbox Code Playgroud)
现在使用JavaScript,这将转换为:
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
);
Run Code Online (Sandbox Code Playgroud)
如您所见,我们首先以正确的顺序构建JSON数组(注意,数组不是对象),然后我们将其转换为String并将其发送到具有参数名称' params' 的适配器.
| 归档时间: |
|
| 查看次数: |
1696 次 |
| 最近记录: |