在我的 ionic 5.0.0 应用程序中,我使用 cordova 的 native HTTP 进行其余调用。下面是我的注销功能的代码片段。
但是当我执行这个函数时,我收到以下错误。
"advanced-http: \"data\" argument supports only following data types: String"
logout() {
this.setData("url", "/web/oauth/revoke-token");
let apiUrl = this.getBaseUrl() + this.getData("url");
const headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Authorization': 'Basic Y2hyR3liUnBxQUR3X2VDemw5dzc0cHU4dXNnYTpKdmZ1azgyYnBUQlVnNDJ6NU1hZFhXOWJPeElh'
};
const params = {
'companyId': this.getData("COMPANY_ID"),
'token': this.getData("ACCESS_TOKEN"),
'client_id': this.getData("CLIENT_ID"),
'token_type_hint': 'access_token'
};
this.nativeHttp.post(apiUrl, params, headers).then(response => {
console.log("success response: "+response);
})
.catch(error => {
console.log("error response: "+error);
});
console.log("finished");
}
这是我接收参数的 Spring Controller 。
@RequestMapping(value = "/oauth/revoke-token", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Object> logout(HttpServletRequest request) {
String clientId = request.getParameter(OAuth2Constants.CLIENT_ID);
String token = request.getParameter(OAuth2Constants.TOKEN);
String tokenTypeHint = request.getParameter(OAuth2Constants.TOKEN_TYPE_HINT);
String companyId = request.getParameter(WebConstants.COMPANY_ID_PARAMETER);
}
但不幸的是,所有参数在 Controller 中接收为空。
有人可以帮助我吗?
最佳答案
终于找到了解决这个问题的方法。在执行 post 调用之前,只需按如下方式设置 http 请求的数据序列化器即可。
this.nativeHttp.setDataSerializer( "urlencoded" );
关于java - 如何将参数从 cordova HTTP 传递到 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184600/