fetch跨域post请求报错Request header field Content-Type is not allowed by Access-Control-Allow-Header

当我在前端进行一个post的跨域请求时,控制台报错Request header field Content-Type is not allowed by Access-Control-Allow-Header。这是因为我在后端设置跨域请求的时候没有所需的请求类型,如图:

app. all( '*', function ( req, res, next) {
//响应头指定了该响应的资源是否被允许与给定的origin共享。*表示所有域都可以访问,同时可以将*改为指定的url,表示只有指定的url可以访问到资源
res. header( "Access-Control-Allow-Origin", "*");
res. header( "Access-Control-Allow-Headers", " X-Requested-With ");
//允许请求资源的方式
res. header( "Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res. header( "X-Powered-By", ' 3.2.1');
res. header( "Content-Type", "application/json;charset=utf-8");
next();
});

,所以直接在代码片段设置一下:

app. all( '*', function ( req, res, next) {
//响应头指定了该响应的资源是否被允许与给定的origin共享。*表示所有域都可以访问,同时可以将*改为指定的url,表示只有指定的url可以访问到资源
res. header( "Access-Control-Allow-Origin", "*");
res. header( "Access-Control-Allow-Headers", " Origin, X-Requested-With, Content-Type, Accept");
//允许请求资源的方式
res. header( "Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res. header( "X-Powered-By", ' 3.2.1');
res. header( "Content-Type", "application/json;charset=utf-8");
next();
});

猜你喜欢

转载自blog.csdn.net/qq_16933879/article/details/80352615
今日推荐