diegomvh / angular-odata

Client side OData typescript library for Angular
https://www.npmjs.com/package/angular-odata
MIT License
50 stars 14 forks source link

$batch with CORS fails #16

Closed koshdim closed 4 years ago

koshdim commented 4 years ago

code sample to reproduce:

const odata = new ODataClient(httpClient, new ODataSettings({
      serviceRootUrl: this.ApiOdataEndPointUrl
    }));

    odata.entitySet('Clients').get().subscribe(console.log); // <-- this works fine
    odata.entitySet('Projects').get().subscribe(console.log); // <-- this works fine

    const batch = odata.batch();
    batch.post(() => {  // <-- this fails
      odata.entitySet('Clients').get().subscribe(console.log);
      odata.entitySet('Projects').get().subscribe(console.log);
    }).subscribe(console.log);

batch produces these errors in Browser's console image

is this a configuration issue? did I miss something?

koshdim commented 4 years ago

I've found the problem. It was in backend ASP.Net Core service, configuration must be in this exact sequence:

            app.UseCors(...);
            app.UseODataBatching();
            app.UseRouting();

then the original problem is resolved.

But I got another, 404 error for internal requests, because the library generates requests like this: GET /Clients HTTP/1.1 and backend couldn't route such requests correctly. requests worked with full path there, but not with relative. I resolved this issue by running requests like this:

batch.post((b) => {
  odata.entitySet( `api/V1/odata/Clients`).get().subscribe(console.log);
  odata.entitySet( `api/V1/odata/Projects`).get().subscribe(console.log);
}).subscribe(console.log);

I would suggest configuration option to specify full path for internal requests.