【问题标题】:angular http client is not working when calling node backend locally?本地调用节点后端时,角度http客户端不起作用?
【发布时间】:2021-06-25 06:55:03
【问题描述】:

尝试使用 angular http 客户端调用 nodejs 本地主机,但它没有命中后端

角度服务.ts

import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({providedIn: 'root'})
export class AdminService {

  constructor(private http: HttpClient) { }

  private  url = "http://localhost:9001/api/saveClients";

  saveClientJobs(data) {
        let options = this.createRequestOptions();
        console.log("Service", data);
        return this.http.post(this.url, data, { headers: options });
    }

    private createRequestOptions() {
        let headers = new HttpHeaders({
            "Content-Type": "application/json"
        });
        return headers;
    }
}

angular proxy.config.json

{
  "/api/*":{
      "target":"http://localhost:9001",
      "secure":false,
      "logLevel":"debug",
      "changeOrigin": true
  }
}

NodeJs 路由

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const admin_controller_1 = require("./api/admin/admin.controller");
class RegisterRouteClass {
    RegisterRoutes(app) {
        app.post('/api/saveClients', admin_controller_1.AdminController.save);
       
    }
}
exports.RegisterRouteClass = RegisterRouteClass;

【问题讨论】:

  • 有任何错误吗?您的网络标签是什么样的?
  • @robbieAreBest 在网络和 nodejs 应用程序中没有任何活动
  • 如果你想使用代理,你的url需要是相对的..
  • @MikeOne 你能提供一个我没有关注的例子吗?抱歉
  • 私人网址 = "localhost:9001/api/saveClients";应该是 private url = "/api/saveClients";,否则你的代理将无法工作..

标签: javascript node.js angular angular-fullstack


【解决方案1】:

为了执行帖子/获取,您需要订阅它。如果您没有订阅 post/get,它将永远不会执行,因此开发者工具的网络选项卡中不会输出任何内容。

saveClientJobs(data).subscribe(result => /* do something with the result */)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-12
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2016-11-07
    相关资源
    最近更新 更多