Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我是否可以在angular 2+;中不使用cookies的情况下向restApi发送json对象;_Angular_Rest_Spring Boot - Fatal编程技术网

我是否可以在angular 2+;中不使用cookies的情况下向restApi发送json对象;

我是否可以在angular 2+;中不使用cookies的情况下向restApi发送json对象;,angular,rest,spring-boot,Angular,Rest,Spring Boot,我试图在一个(angular2+springMvc+java)web项目中向rest服务发送一个json类型的对象,但这似乎非常困难。我也不会用饼干 是的,请记住设置正确的内容类型标题 constructor(http: HttpClient) { let headers = new HttpHeaders(); headers = headers.set('Content-Type', 'application/json'); this.http.post(<ur

我试图在一个(angular2+springMvc+java)web项目中向rest服务发送一个json类型的对象,但这似乎非常困难。我也不会用饼干

是的,请记住设置正确的内容类型标题

constructor(http: HttpClient) {
    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json');

   this.http.post(<url>, {jsonData}, { headers });
}
构造函数(http:HttpClient){
let headers=新的HttpHeaders();
headers=headers.set('Content-Type','application/json');
this.http.post(,{jsonData},{headers});
}

正如我从您的问题中了解到的,您正在试图找到一种在您的Angular项目中处理http请求的方法

让我们先看看您的项目结构。您必须有一个单独的目录,其中包含与给定模块相关的所有服务

在该目录中,您可以使用
ng g s service name
创建服务,在本例中可以处理http请求

import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {environment} from "../../../../environments/environment";

const BASE_URL = environment.API_PATH;
const WAR = environment.API_WAR;

@Injectable({
  providedIn: 'root'
})
export class ServiceNameService {
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': 'my-auth-token'
    })
  };

  constructor(private http: HttpClient) {
  }

  getCall() {
    return this.http.get(`${BASE_URL}/${WAR}/your/path/all`);
  }

  getByIdCall(id) {
    return this.http.get(`${BASE_URL}/${WAR}/your/path?id=${id}`);
  }

  deleteByIdCall(id) {
    return this.http.delete(`${BASE_URL}/${WAR}/your/path/delete?id=${id}`);
  }

  postCall(payload: any) {
    return this.http.post(`${BASE_URL}/${WAR}/your/path/save`, payload);
  }

  putCall(id, payload) {
    return this.http.put(`${BASE_URL}/${WAR}/your/path/update?id=${id}`, payload);
  }

}
import {Component, OnInit} from '@angular/core';
import {ServiceNameService} from '../../../services/http-services/service-name.service';

@Component({
  selector: 'app-config-view',
  templateUrl: './config-view.component.html',
  styleUrls: ['./config-view.component.scss']
})
export class ConfigViewComponent implements OnInit {

  constructor(private serviceNameService: ServiceNameService) {
  }

  ngOnInit() {
  }

  loadAll() {
    this.serviceNameService.getCall()
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  loadById(id) {
    this.serviceNameService.getByIdCall(id)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  deleteById(id) {
    this.serviceNameService.deleteByIdCall(id)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  save() {
    const payload = {
      test: "test value"
    }
    this.serviceNameService.postCall(payload)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  update() {
    const payload = {
      test: "test value updated"
    }
    this.serviceNameService.putCall(id, payload)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

}
现在您必须在组件中调用它,您需要执行http请求

import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {environment} from "../../../../environments/environment";

const BASE_URL = environment.API_PATH;
const WAR = environment.API_WAR;

@Injectable({
  providedIn: 'root'
})
export class ServiceNameService {
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': 'my-auth-token'
    })
  };

  constructor(private http: HttpClient) {
  }

  getCall() {
    return this.http.get(`${BASE_URL}/${WAR}/your/path/all`);
  }

  getByIdCall(id) {
    return this.http.get(`${BASE_URL}/${WAR}/your/path?id=${id}`);
  }

  deleteByIdCall(id) {
    return this.http.delete(`${BASE_URL}/${WAR}/your/path/delete?id=${id}`);
  }

  postCall(payload: any) {
    return this.http.post(`${BASE_URL}/${WAR}/your/path/save`, payload);
  }

  putCall(id, payload) {
    return this.http.put(`${BASE_URL}/${WAR}/your/path/update?id=${id}`, payload);
  }

}
import {Component, OnInit} from '@angular/core';
import {ServiceNameService} from '../../../services/http-services/service-name.service';

@Component({
  selector: 'app-config-view',
  templateUrl: './config-view.component.html',
  styleUrls: ['./config-view.component.scss']
})
export class ConfigViewComponent implements OnInit {

  constructor(private serviceNameService: ServiceNameService) {
  }

  ngOnInit() {
  }

  loadAll() {
    this.serviceNameService.getCall()
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  loadById(id) {
    this.serviceNameService.getByIdCall(id)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  deleteById(id) {
    this.serviceNameService.deleteByIdCall(id)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  save() {
    const payload = {
      test: "test value"
    }
    this.serviceNameService.postCall(payload)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

  update() {
    const payload = {
      test: "test value updated"
    }
    this.serviceNameService.putCall(id, payload)
      .subscribe((data: any) => {
          console.log(data);
        }, error => {
          console.log(error);
        }
      );
  }

}
现在,您可以根据需要调用这些方法

希望这有帮助


祝你好运

添加更多信息,并尝试告诉您尝试了什么以及isuuesHere:我使用此指南来获得有关可能性的概述,但别忘了阅读以下内容:问题:我无法将json对象从angular端传递到rest服务。另外,从rest获取该对象后,需要进行一些修改,然后再次将其发送到后端