[英]How to pass parameters from cordova HTTP to Spring controller
在我的 ionic 5.0.0 應用程序中,我使用科爾多瓦的原生 HTTP 進行 rest 調用。 下面是我注銷 function 的代碼片段。
但是當我執行這個 function 我得到以下錯誤。
"advanced-http: \"data\" 參數僅支持以下數據類型:字符串"
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 中接收為 null。
有人能幫我嗎?
最后我找到了解決這個問題的方法。 在進行 post 調用之前,只需為 http 請求設置數據序列化器,如下所示。
this.nativeHttp.setDataSerializer( "urlencoded" );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.