Sending An OPTIONS Request With Angular 2

The Angular 2 Http class does not offer an OPTIONS convenience method out-of-the box. Interestingly, all the major HTTP methods are supported: GET, POST, PUT, delete, patch and head, but the OPTIONS was not implemented. To perform an e.g. POST request, you can use the built-in method:


import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';

export default class XYZService{
   
    constructor(private http:Http){}

    performPost(uri,payload):Promise<Workshop[]>{
        return this.http.post(uri,payload).
        toPromise().
        then(r => r.json()).
        catch(this.handleError);
    }

   handleError(error){
        return Promise.reject(error.messages || error);
    }
//(...)

For the initiation of an OPTIONS request you will have to rely on the generic request method of the same Http class:

    options(uri):Promise{
        return this.http.request(uri,{method:'OPTIONS'}).
        toPromise().
        then(r => r.json()).
        catch(this.handleError);
    }

See you at Java EE Workshops at Munich Airport, Terminal 2 and particularly at Building Angular 2 Applications or Building React Applications.


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License