我正在将我的应用程序从MFP V 7.1迁移到MFP V 8.0。我有以下场景,我正在构建一个数据对象,并使用以下代码从我的客户端向MFP适配器发送请求:
function SearchData(username,name,city){
this.username = username;
this.name = name;
this.city = city;
}
searchData(){
var searchData = new SearchData(getUserId(),name,city);
var dataRequest = new WLResourceRequest('/adapters/MyAdapterName/searchEmployer', WLResourceRequest.GET);
alert('there');
dataRequest.send(searchData).then(
function(response){
console.log('response --> ' + response);
},
function(){
console.log('error response --> ' );
}
);
}
以下是我的java脚本适配器中编写的代码:
function searchData(searchData){
try{
WL.Logger.info("Inside searchData() method.");
var input = {
method : 'post',
returnedContentType : 'json',
path : 'rest/search',
body : {
contentType : 'application/json;charset=utf-8',
content : JSON.stringify(searchData)
}
};
var response = MFP.Server.invokeHttp(input);
return response;
}catch(exception){
WL.Logger.error("Inside searchData() method :: " + exception.message);
throw exception;
}
}
每当我调用此方法时,都会调用失败函数。我还尝试使用sendFormParameters方法发送请求,但它在错误后返回打印:
worklight.js:9342 Uncaught Error: Invalid invocation of method WLResourceRequest.sendFormParameters; Form value must be a simple type.
logAndThrowError @ worklight.js:9342
encodeFormParameters @ worklight.js:9727
WLResourceRequest.sendFormParameters @
worklight.js:9685searchEmployer @ VM79:47
onclick @ index.html:1
答案 0 :(得分:4)
当您传递的对象包含嵌套对象或类型为函数时,会出现此问题。
在您的情况下,您有多个具有相同名称searchData
,SearchData
的变量/函数。我建议您将名称更改为更具描述性。
我尝试了以下内容并成功运行
function SearchData(username,name,city){
this.username = username;
this.name = name;
this.city = city;
}
function search(){
var data = new SearchData(getUserId(), name, city);
var request = new WLResourceRequest('/adapters/MyAdapterName/searchEmployer', WLResourceRequest.GET);
request.send(searchData).then(function(response){
console.log('response --> ', response);
}, function(error){
console.log('error response --> ', error);
});
}
如果您在运行上述代码后仍遇到问题,请与您的项目共享链接或添加更多代码段。
<强> UDPATE:强>
对于Javascript适配器,通过GET变量params
接收参数,params
是一个参数数组。
因此,您需要在客户端更新代码,如下所示:
function search(){
var data = new SearchData(getUserId(), name, city);
var request = new WLResourceRequest('/adapters/MyAdapterName/searchEmployer', WLResourceRequest.GET);
request.setQueryParameter("params", [JSON.stringify(data)]);
request.send().then(function(response){
console.log('response --> ', response);
}, function(error){
console.log('error response --> ', error);
});
}
由于传递给适配器的数据是一种刺痛,您需要更新适配器程序以反映该情况,即删除JSON.stringify
function searchData(payload){
try{
WL.Logger.info("Inside searchData() method.");
var input = {
method : 'post',
returnedContentType : 'json',
path : 'rest/search',
body : {
contentType : 'application/json;charset=utf-8',
content : payload
}
};
var response = MFP.Server.invokeHttp(input);
return response;
}catch(exception){
WL.Logger.error("Inside searchData() method :: " + exception.message);
throw exception;
}
}