角度HTTP POST请求 [英] Angular HTTP POST request

查看:79
本文介绍了角度HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular2和Spring-MVC中构建了一个应用程序,当我尝试向服务器发出POST请求时,我没有收到任何成功或失败的迹象,但是该请求没有发生,因为我无法查看新数据.当我从Postman发出请求时-请求成功,并且可以看到新数据.

I build an app in Angular2 and Spring-MVC, and when I try to make a POST request to my server I don't get any sign of success or fail, but the request doesn't happening because I can't see the new data. When I do the request from Postman - the request is successful and I can see the new data.

Angular2代码:

The Angular2 code:

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions  } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';

@Injectable()
export class MainContentService {
  constructor(
    private http: Http) {}

  addNewCategory(categoryName: string) {
    let body = JSON.stringify({ name: categoryName });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    console.log(body);
    console.log(options);

    return this.http.post('http://localhost:8080/api/v1/categories', body, options)
              .map(this.extractData)
              .catch(this.handleError);
  }

  private extractData(res: Response) {
    let body = res.json();
    console.log(body);
    return body.data || { };
  }

  private handleError (error: any) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}

我可以在dev-tools控制台中看到console.log(body)console.log(option),但仅此而已:

I can see the console.log(body) and console.log(option) printed in the dev-tools console, but nothing more then that:

邮递员要求:

我的组件:

import { Component } from '@angular/core';

import { MainContentService } from '../shared/main-content.service';

@Component({
  moduleId: module.id,
  selector: 'bfy-add-category',
  templateUrl: 'add-category.component.html',
  styleUrls: ['add-category.component.css'],
  providers: [MainContentService]
})
export class AddCategoryComponent {
  editName: string;

  constructor(
    private mainContentService: MainContentService
  ) { }

  cancel() {
    console.log('cencel');
  }

  save() {
    let categoryName = this.editName;
    this.mainContentService.addNewCategory(categoryName);
  }
}

我的组件HTML代码:

My component HTML code:

<div class="col-sm-12 col-md-8 col-lg-9 thumbnail pull-right">
  <label>Category: </label>
  <input [(ngModel)]="editName" placeholder="Category name .."/>

  <div>
    <button (click)="cancel()">Cancel</button>
    <button (click)="save()">Save</button>
  </div>
</div>

推荐答案

只要有人订阅了Observable,http.get/post/...方法就会等待.在此之前,它不会发出请求.这称为cold observable.订阅是这样的:

The http.get/post/... methods wait as long as someone subscribes to the Observable. It won't make a request before that happens. This is called a cold observable. Subscribing works like that:

http.get("http://yoururl.com").subscribe(data => { ... });

这篇关于角度HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆