0

I have the following code:

  var headers = new Headers();
                            // headers.append('Content-Type', 'application/json');
                            headers.append('Content-Type', 'application/x-www-form-urlencoded');
                            this.http.post(
                                'http://192.168.1.45:3000/testrestapi',
                                {headers: headers}
                            ).map((res => res.json())
        .subscribe(data => {
          // we've got back the raw data, now generate the core schedule data
          // and save the data for later reference
          this.data = data;
      console.log('Friends Provider was a success!');
      console.log(JSON.stringify(data));
          resolve(this.data);
        },
    (err)=>{
        console.log('Error in Friends Provider!');
    },
    ()=>{
           console.log('Friends Provider network call has ended!');
    }
        )

       )
   });

The code compiles fine without error, but I get the following errors in my ide:

enter image description here

I am following this docs: https://angular.io/docs/js/latest/api/http/Http-class.html . It shows how to use HTTP for GET, but not for POST. It seems I am missing the type for my headers which I am unsure what to put here as well it complains about the type for the subscribe method and again I am unsure what to put here as I am following the docs and it does not seem to have anything different?

2
  • is it complete code? Commented Mar 21, 2016 at 5:19
  • compiles fine without error and then you show a picture of compile errors? You import Http twice like the error says, for starters. Read the errors and fix the simplest ones at least first. Commented Mar 21, 2016 at 5:36

3 Answers 3

1

You have imported Http module 2 times in 2nd and 3rd lines of your code, one of them is redundant.

API for http post is:

post(url: string, body: string, options?: RequestOptionsArgs) : Observable<Response>

in https://angular.io/api/http/Http

it expects a JSON.stringified body as the second parameter but you are pushing the header options which should be the third parameter.

I also suggest using

headers.set('Content-Type', 'application/json'); 

instead of append.

Also you are missing the paranthesis in subscribe after data.

.subscribe(
    (data) => {
        console.log('Do something with data here \n',data);
    }
);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Is there a reason why set is better than append?
Because if you use the method that your http request and header.append is in more than once, you will append the header 2 times :)
0
 var headers = new Headers();
                            // headers.append('Content-Type', 'application/json');
       headers.append('Content-Type', 'application/x-www-form-urlencoded');
       this.http.post(
                       'http://192.168.1.45:3000/testrestapi',
                                {headers: headers}
                      )
        .map((res => res.json())
        .subscribe(data => 
         {

          this.data = data;
          console.log('Friends Provider was a success!');
          console.log(JSON.stringify(data));
          //resolve(this.data);
          },
        (err)=>{
        console.log('Error in Friends Provider!');
        },
        ()=>{
           console.log('Friends Provider network call has ended!');
        }

       );

Comments

0

The code compiles fine without error,

Just because you get valid JavaScript doesn't mean that it compiled without error. This is actually a TypeScript feature (see why typescript).

The errors you are seeing in the error are the compiler errrors

Comments

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.