angular 中配置跨域

1. 新建一个 proxy.conf.json

{
    "/search": {
        "target""https://wenku.baidu.com/search",
        "secure"false,
        "changeOrigin"true
    },
    "/getnotice": {
        "target""https://wenku.baidu.com/message/getnotice",
        "secure"false,
        "changeOrigin"true
    },
    "/api": {
        "target""https://wenku.baidu.com",
        "secure"false,
        "changeOrigin"true
    }
}

2. 在 package.json 中

{
  "name""angular-demo",
  "version""0.0.0",
  "scripts": {
    "ng""ng",
    "start""ng serve --proxy-config proxy.config.json",
    "build""ng build",
    "test""ng test",
    "lint""ng lint",
    "e2e""ng e2e"
  },
  "private"true,
  "dependencies": {
    "@angular/animations""~8.1.1",
    "@angular/common""~8.1.1",
    "@angular/compiler""~8.1.1",
    "@angular/core""~8.1.1",
    "@angular/forms""~8.1.1",
    "@angular/platform-browser""~8.1.1",
    "@angular/platform-browser-dynamic""~8.1.1",
    "@angular/router""~8.1.1",
    "axios""^0.19.0",
    "redux""^4.0.4",
    "rxjs""~6.4.0",
    "tslib""^1.9.0",
    "zone.js""~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular""~0.801.1",
    "@angular/cli""~8.1.1",
    "@angular/compiler-cli""~8.1.1",
    "@angular/language-service""~8.1.1",
    "@types/node""~8.9.4",
    "@types/jasmine""~3.3.8",
    "@types/jasminewd2""~2.0.3",
    "codelyzer""^5.0.0",
    "jasmine-core""~3.4.0",
    "jasmine-spec-reporter""~4.2.1",
    "karma""~4.1.0",
    "karma-chrome-launcher""~2.2.0",
    "karma-coverage-istanbul-reporter""~2.0.1",
    "karma-jasmine""~2.0.1",
    "karma-jasmine-html-reporter""^1.4.0",
    "protractor""~5.4.0",
    "ts-node""~7.0.0",
    "tslint""~5.15.0",
    "typescript""~3.4.3"
  }
}

 

posted @ 2019-10-04 12:25  monkey-K  阅读(2446)  评论(0编辑  收藏  举报