0

I cannot interecept error from http post

a part of my mservice (http post method)

addApplicationLink(applicationLink: ApplicationLink){
    let body = JSON.stringify(applicationLink);
    let requestHeaders = new Headers();
    var headers = new Headers();
    headers.set('Content-Type',  ['application/json; charset=utf-8']);
     let reqoptions =  new RequestOptions({
        headers: headers
    });
    return this._http.post(this._applicationLinksUrl + this._linkServicePath,body,{headers: headers});

in my component :

addApplicationLink() {
    //todo
    this.addNewLink = false;
    /* check if must be done after call real rest service */
    //this.applicationLinks.push(this.applicationLinkAdd);
    this._applicationLinkService.addApplicationLink(this.applicationLinkAdd)

        .subscribe(data => {
            this.applicationLinks.push(this.applicationLinkAdd)
        },
        error => {
            // handle error
            console.error('this an erreor ' + error.status)
        }
        )

When user tries to add two same applicationlinks , the backend returns an error 409 But when I execute , error.status displays 200 in browser console I see also in browser console XMLHttpRequest cannot load http://localhost:7001...... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 409.

rem : Http post is made with json , thus there is a prefligth call

Have you an idea to intercept error 409 ?

1
  • If your service is made with NodeJS and Express, then you can use this gem: npmjs.com/package/cors Commented May 30, 2016 at 11:21

1 Answer 1

2

In fact, your server doesn't send back the CORS header (Access-Control-Allow-Origin' header is missing). This prevent the browser from providing the actual 409 error to the Angular2 application within the browser.

You need to fix first the problem on the server and you will be able to see this 409 error.

For more details about how CORS works, you could have a look at this article:

Sign up to request clarification or add additional context in comments.

1 Comment

I have found the probleme, for response 409 , headers are not set. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.