发表于: 2020-05-13 23:36:11

1 1993


今日完成

完成跨域问题,使用post,get


写了个demo练习跨域post请求一直404

浪费了大把时间,没有找到问题,

新建了个demo,同样配置操作,请求正常


在请求前先使用postman测试接口是否正常,在这里卡了半天才发现是地址有问题

 http://175.25.23.192:10498/该地址无法使用

发送请求时终端会报错


配置跨域并发送请求

在项目根目录创建配置文件 proxy.config.json  

启动项目使 使用 ng serve --proxy-config proxy.config.json

{

    //url拦截指向target

    "/api": {

        "target": "http://dev.admin.carrots.ptteng.com",

        //为https协议是为true

        "secure": false,

         //跨域时为true

        "changeOrigin": true,

         //url重定向  

        "pathRewrite": {
            "^/api": "" //替换为空
        }
    }
}

跟组件app.module.ts引用注入httpclient

import { HttpClientModule } from '@angular/common/http';


 imports: [
    BrowserModule,

    AppRoutingModule,


    HttpClientModule,


  ],


在应用类组件中调用 并在constructor依赖注入

import { HttpClient, HttpHeaders } from '@angular/common/http';
constructor(public http: HttpClient) { }


在方法中调用get 并打印返回值

  this.http.get("/api/a/article/search").subscribe(res => {
      console.log(res);
    })




返回列表 返回列表
评论

    分享到