首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用组件中的角服务将表单数据发布到api

如何使用组件中的角服务将表单数据发布到api
EN

Stack Overflow用户
提问于 2019-03-07 00:03:46
回答 1查看 850关注 0票数 1

我有一个在角6的表单,我也有一个服务,用来获取和发布请求到一个web。我可以得到罚款的请求。但是,我很难将我的数据从我的表单发布到api。我的代码不起作用。到目前为止,我已经:

service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';


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

  serviceApiUrl: string = 'api/incident';


  constructor(
    private http: HttpClient,

  ) { }

  getAll(): Observable<any> {
    return this.http.get<any>(this.serviceApiUrl)
      .pipe(
        catchError(this.handleError)
      );
  }

  getIncidents(customerId): Observable<any> {
    return this.http.get<any>(this.serviceApiUrl + "?customer_id=" + customerId)
      .pipe(
        catchError(this.handleError)
      );
  }

  postIncidents(customerId): Observable<any> {
    return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId, null)
      .pipe(catchError(this.handleError));
  }

  private handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      console.log(error.error.message)

    } else {
      console.log(error.status)
    }
    return throwError(
      console.log('Something has wrong; Api is not working!'));
  };

}

form.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { FormGroup, FormControl } from '@angular/forms';
import { Validators } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
import { Request } from '../../models/request.model'
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AppComponent } from '../../../app.component';
import { ServicenowService } from '../../services/servicenow.service';
import { HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse,} from '@angular/common/http';

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

  public customers;
  public customer;

  private customer_id = 7; /// this.appComponent.customer_id;

  serviceForm;

  countries = [
    { name: 'Choose an option' },
    { name: 'United Kingdom', },
    { name: 'United States of America', },
    { name: 'Russia', },
    { name: 'Moscow', },
    { name: 'Africa', },
  ];

  users = [
    { id: 'Select an option', },
    { id: '1', },
    { id: '2', },
    { id: '3', },
  ];

  devices = [
    { id: 'Select an option', },
    { id: '1', },
    { id: '2', },
    { id: '3', },
  ];

  constructor(private service: ServicenowService,
    private appComponent : AppComponent,
    private router: Router,
    private http: HttpClient

  ) {
  }

  ngOnInit() {
    this.serviceForm = new FormGroup({
      u_caller_id: new FormControl(this.users[0], Validators.required),
      u_cmdb_ci: new FormControl('', Validators.required),
      u_destination_country: new FormControl(this.countries[0], Validators.required),
      u_requester_phone_number: new FormControl('', Validators.required),
      u_serial_number: new FormControl(this.devices[0], Validators.required),
      subject: new FormControl('', Validators.compose([
        Validators.required,
        Validators.minLength(5),
        Validators.maxLength(10)
      ])),
      issue: new FormControl('', Validators.required),
    });
  }

  onSubmit() {
    var data = "u_cmdb_ci=" + this.serviceForm.value.u_cmdb_ci;
    this.service.postIncidents(this.customer_id).subscribe((data) => {});
    console.log("data has gone");
       }
  }

我已经将服务注入到组件中,但它不起作用--我还尝试了以下几个方面,我知道它只有一个字段,但我只想测试它,但没有工作:

代码语言:javascript
复制
onSubmit() {
    var data = "u_cmdb_ci=" + this.serviceForm.value.u_cmdb_ci;
    this.http.post("api/incident", data).subscribe((res) => {});
    console.log("data has gone");
    }
EN

回答 1

Stack Overflow用户

发布于 2019-03-07 00:13:28

您应该通过投递您的对象,而不是试图将其转换为URL param。此外,还添加了一个错误处理程序,这样您就可以获得一些错误数据来解决任何问题。

代码语言:javascript
复制
onSubmit() {
    var data = this.serviceForm.value.u_cmdb_ci;
    this.http.post("api/incident", data).subscribe(
       (res) => {
          console.log(res);
       },
       (error) => {
          console.log(error);
       }
    );
    console.log("data has gone");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55034094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档