简体   繁体   中英

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:



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:

  • service reached
  • month: 3
  • id: 1
  • url:http://localhost:8080/products/extenddate/3/1

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.

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;
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM