Vue.js和Nodejs的CORS问题

时间:2019-01-10 10:55:58

标签: node.js vue.js cors

我将vuejs与nodejs一起使用,其vue客户端地址为http://localhost:8000,nodejs服务器地址为http://localhost:3000

调用api时,我收到错误提示CORS preflight channel did not succeed request headers

请求

Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Host:localhost: 3000 
Origin:http://localhost: 8000

响应

Access-Control-Allow-Headers: Origin, X-Requested-With, Accept,content-type
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT,PATH
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Content-Type:application/json; charset=utf-8

响应错误为NS_ERROR_DOM_BAD_URI

1 个答案:

答案 0 :(得分:1)

如果使用import { Injectable } from '@angular/core'; import { NgbDateParserFormatter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; @Injectable() export class NgbDateCustomParserFormatter extends NgbDateParserFormatter { parse(value: string): NgbDateStruct { return {year: 2019, month: 1, day: 1}; } format(date: NgbDateStruct): string { return '01-01-2019'; } } ,则只需使用nodejs cors lib即可在节点服务器中启用CORS。

我强烈建议您仅出于开发目的激活它:

expressjs