flutter---dio解析错误的解决办法
(2021-05-10 17:33:22)| 分类: Flutter-Dart | 
Flutter dio XMLHttpRequest error
dio网络请求app端请求接口没有问题,换成web端请求报错XMLHttpRequest
error
这是浏览器限制跨域问题,通常解决方案如下:
服务端添加Access-Control-Allow-Origin
这是浏览器限制跨域问题,通常解决方案如下:
服务端添加Access-Control-Allow-Origin
这里我后端用golang中的gin  增加中间件进行添加 
package mid
import (
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
// 跨域
func Cors() gin.HandlerFunc {
return cors.New(
cors.Config{
AllowAllOrigins:  true, 
AllowMethods:   
 []string{"GET", "POST", "PUT", "DELETE",
"OPTIONS"},   
AllowHeaders:   
 []string{"*"},   
ExposeHeaders:   
[]string{"Content-Length", "text/plain", "Authorization",
"Content-Type"},  
AllowCredentials: true,
MaxAge:     
     12 *
time.Hour,      
},
)
}
其中gin中的post传值有两种
form方式的请求:
json方式的请求:
data,
_ := ioutil.ReadAll(ctx.Request.Body)
fmt.Printf("ctx.Request.body:
%v", string(data))
flutter传递map相当于json方式

 加载中…
加载中…