Parcourir la source

优化:request传参data时过滤null值

萤火科技 il y a 1 an
Parent
commit
bfdf12b437
2 fichiers modifiés avec 7 ajouts et 2 suppressions
  1. 1 1
      pages/checkout/index.vue
  2. 6 1
      utils/request/index.js

+ 1 - 1
pages/checkout/index.vue

@@ -370,7 +370,7 @@
         this.getOrderData()
       },
 
-      // 显示积分说明
+      // 显示积分抵扣说明
       handleShowPoints() {
         this.showPoints = true
       },

+ 6 - 1
utils/request/index.js

@@ -2,6 +2,7 @@
  * request插件地址:https://ext.dcloud.net.cn/plugin?id=822
  */
 import store from '@/store'
+import { hasOwnProperty, isEmptyObject, filterNull } from '@/utils/util'
 import request from './request'
 import Config from '@/core/config'
 
@@ -18,7 +19,7 @@ const $http = new request({
   defaultUploadUrl: 'upload/image',
   // 设置请求头(如果使用报错跨域问题,可能是content-type请求类型和后台那边设置的不一致)
   header: {
-    'content-type': 'application/json;charset=utf-8'
+    'Content-Type': 'application/json;charset=utf-8'
   },
   // 请求超时时间, 单位ms(默认15000)
   timeout: 15000,
@@ -71,6 +72,10 @@ $http.requestStart = options => {
   // #ifdef H5
   options.header['domain'] = window.location.hostname
   // #endif
+  // 剔除data中为null的属性 (解决传参字符串'null'的问题)
+  if (hasOwnProperty(options, 'data') && !isEmptyObject(options.data)) {
+    options.data = filterNull(options.data)
+  }
   // return false 表示请求拦截,不会继续请求
   return options
 }