0

When I called the rest API from postman it gives the actual result but when I do it from angular 6 it return every response as error.

        this.code=this.codeCollectorForm.value;
        let formData: FormData = new FormData();    
        formData.append('value', this.code.value);
        formData.append('input', this.code.input);
      this.httpclient.post("http://localhost:8000/onlineide/runcode",formData).subscribe(
       (data) => {
          $('#output').val("okk");

       }, error => {

          $('#output').val("Not okk");

        }
       );

Everything is accepted as error. Any solution please?. The error always shown is

Http failure response for http://localhost:8000/onlineide/runcode: 0 Unknown Error
enter code here
mahbubcseju
  • 2,200
  • 2
  • 16
  • 21

1 Answers1

1

This could happen when you get cross origin issues. Cross origin is something enforced by client side (by browsers) The reason why postman is not getting affected by that is, postman is doing the call ignoring the cross origin by default.

If your backend is kind of a restful service that could be used from multiple clients, you can enable CORS in your backend.

However if you plan to use it only from one client you better avoid access from irrelevant origins(do not enable CORS), in this case you should be serving the angular application on the same origin. You can still have the back-end and front-end seperatly served in your local by establishing a proxy in between for the sake of development: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

talhature
  • 2,246
  • 1
  • 14
  • 28