Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call from angular frontend to java backend fails without error

I’m building a project in angular which connects to my java-backend (restapi). The project basics are already done, but now I’m adding one by one some advanced calls. Suddenly, I come to the point where I’m implementing a new method, but therequest just doesn’t reach the method in the controller. I don’t even get an error in the frontend, the backend just doesn’t react. The frontend service creates the right url, makes the call, no error .. backend receives nothing.

I tried changing the request to get, losing the pathvariables and using RequestBody, creating a simple test-controller that returns a String, .. nothing seems to work anymore, except what was already their. What am I doing wrong ? Why doesn’t this specific request doesn’t reach my backend?

angular service:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel



const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  })
};

@Injectable({
  providedIn: 'root'
})

export class ProductService {
url = "http://localhost:8080/products/";

  constructor(private http: HttpClient) { }

  extendDateWithMonths(month : number, id:number | undefined): Observable<Response> {
    console.log("service reached");
    console.log("month: " + month);
    console.log("id: " + id);
    const specificUrl = this.url + "extenddate/"+ `${month}` +"/"+ `${id}`;
    console.log("url: " + specificUrl);
    return this.http.post<any>(specificUrl, month, httpOptions);
   
  }
}

console.out request:

java backend controller running on localhost 8080:


@RestController
@RequestMapping("/products")
@CrossOrigin(exposedHeaders = "http://localhost:4200")
public class ProductController {

    @Autowired
    ProductService productService;

    @PostMapping("/extenddate/{month}/{id}")
    public ResponseEntity<?> extenddate(@PathVariable int month, @PathVariable long id){
        System.out.println("extenddate reached");
        productService.extendProductsFromBon(month, id);
        return ResponseEntity.ok(new MessageResponse("date extended successfully!"));
    }
}

Why doesn’t this specific request doesn’t reach my backend? Why don’t I get an error or anything from the console? Either it can reach the restapi or it cannot, but this is very strange.

>Solution :

You call the method, that is why you see the console logs. The return value, however is a cold observable. In order to actually send the request to the backend you need to subscribe to the observable.

I expect that you simply call the method like this somewhere:

const response = this.productService.extendDateWithMonths(1, 2);

What you need to do:

this.productService.extendDateWithMonths(1, 2).subscribe(
  response => this.response = response;
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading