angular 中配置跨域

Posted monkey-k

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"
  

 

以上是关于angular 中配置跨域的主要内容,如果未能解决你的问题,请参考以下文章

Angular 10 应用程序不会在跨域请求中发送 JWT 刷新令牌

跨域ajax调用失败-Angular.js节点js + express

Angular配置proxy代理

Angular4 后台管理系统搭建(10) - 做一个通用的可跨域上传文件的组件

从零开始一个个人博客 by asp.net core and angular(三)

Sails/Angular 跨域请求被阻止