0

1.I can't setup a proper request, with useCredentials.Can anyone tell me what's wrong with the code?

import { Injectable }     from '@angular/core';
import { Http, Response,Headers, RequestOptions } from '@angular/http';

import { Observable }     from 'rxjs/Observable';

@Injectable ()
export class StructureRequestService {
result: Object;
constructor (private http: Http) {

2.use XHR object - I think the problem is here.

    let _build = (<any> http)._backend._browserXHR.build;
    (<any> http)._backend._browserXHR.build = () => {
        let _xhr =  _build();
        _xhr.withCredentials = true;
        return _xhr;
    };
}
private myUrl = 'http://manny.herokuapp.com/audit/get/structure';

//create an http request
sendRequest() {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({
        headers: headers
       // , withCredentials: true
    });
    return this.http.post(this.myUrl,options)
        .map((res: Response) => res.json())
        .subscribe(res => {this.result = res;});
}
}
Serhiy
  • 1,893
  • 9
  • 30
  • 48

1 Answers1

0

You are missing the post body parameter in your post request. It must be a string

 return this.http.post(this.myUrl, body, options)

Body

Despite the content type being specified as JSON, the POST body must actually be a string. Hence, we explicitly encode the JSON hero content before passing it in as the body argument.

ref: https://angular.io/docs/ts/latest/guide/server-communication.html

eko
  • 39,722
  • 10
  • 72
  • 98
  • oh. thanks.What if I don't have body param to send? And how about withCredentials: true part ? is it done correct? – Serhiy Jun 01 '16 at 13:14
  • @Serhiy check this post about that subject http://stackoverflow.com/q/35007572/5706293 – eko Jun 01 '16 at 13:16
  • cool. your advice works. I just missed body, I used that in Angular 1 I sent this request without body, just like this. And I could'n find what is a problem. You helped me a lot. thank you!!! – Serhiy Jun 01 '16 at 13:21
  • i tried this but still getting a 401 on put. gets are going through no problem – Sonic Soul Nov 30 '16 at 19:49