ionic3中post请求乱码处理

2018-11-09  本文已影响0人  竹子_331a

我的开发环境是jdk8+tomcat7,为了统一,方便管理,我们将tomcat的默认编码设置为utf-8。但是在用ionic3开发手机版的时候,发现采用get请求时无乱码,但是一旦使用post请求,则传递的中文参数会变成乱码,找了一些资料,也没有一点头绪。最后灵光一闪,试着在头部加入了编码,果然可以了,分享一下~
关键代码参考如下:

              let params = new HttpParams()
                .set('id',that.todoItem.processInstanceId)
                .set('mySuggestion',that.mySuggestion)
                .set('nextNodeId',that.nodeId)
                .set('nextNodeName',that.nodeName)
                .set('loginFrom', 'app');
              let url=GlobalVariable.BASE_URL + 'pad.do?method=completeTaskNew';
              that.http.post(url,params, {
                withCredentials: true, //跨域操作
                headers: {
                  'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',   //此处设置编码格式
                }
              }).subscribe(res => {
                let result=res['msg'];
                if(result=='Y'){
                  let alert = this.alertCtrl.create({
                    title: '提示',
                    subTitle: '流程已成功处理!',
                    buttons: ['OK']
                  });
                  alert.present();
                  //that.navCtrl.pop();
                  that.navCtrl.pop().then(() => {
                    let msg="Y";
                    this.navParams.get('callback')(msg);
                  });

                }else{
                  let alert = this.alertCtrl.create({
                    title: '提示',
                    subTitle: '流程数据异常,请在电脑端处理!',
                    buttons: ['OK']
                  });
                  alert.present();
                }

                },
                response => {
                  console.log("PUT call in error", response);
                });
上一篇下一篇

猜你喜欢

热点阅读