0

The batch request for WebAPI services triggered correctly for get, put, post and delete method.

When the go ahead and run the same service for the cross origin i am having the following issue.

Failed to load http://localhost:63187/api/batch: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:56126' is therefore not allowed access. The response had HTTP status code 400.

The ajax request will be like this

{
contentType:
"multipart/mixed; boundary="batch_9de42638-127f-4f00-b2df-1dd93b8b8eeb""
data:
"--batch_9de42638-127f-4f00-b2df-1dd93b8b8eeb
↵Content-Type: application/http; msgtype=request
↵
↵PUT http://localhost:63187/put HTTP/1.1
↵Content-Type: application/json; charset=utf-8
↵Host: localhost:56126
↵
↵{"EmployeeID":4,"FirstName":"kalai","LastName":"Peacock","City":"London"}
↵--batch_9de42638-127f-4f00-b2df-1dd93b8b8eeb
↵Content-Type: application/http; msgtype=request
↵
↵PUT http://localhost:63187/put HTTP/1.1
↵Content-Type: application/json; charset=utf-8
↵Host: localhost:56126
↵
↵{"EmployeeID":2,"FirstName":"fdsfsda","LastName":"Fuller","City":"London"}
↵--batch_9de42638-127f-4f00-b2df-1dd93b8b8eeb--
↵"
type: "POST"
url: "http://localhost:63187/api/batch"
}

As well as i have enable the cross origin in the webapi service.

I have installed webapi cross origin support package.

Install-Package Microsoft.AspNet.WebApi.Cors

and i have enabled the cross origin in webapi register as below.

 config.EnableCors();

In the webapi controller i have enabled the all origin support

[EnableCors(origins: "*", headers: "*", methods: "*")]

Where did i made a mistake.

Kalai
  • 287
  • 1
  • 5
  • 17

1 Answers1

0

By Adding Header Information in Web.config may solve your issue

<system.webServer>
 <httpProtocol>
   <customHeaders>
     <add name="Access-Control-Allow-Origin" value="*" />
     <add name="Access-Control-Allow-Headers" value="Content-Type" />
     <add name="Access-Control-Allow-Methods" 
      value="GET,POST,PUT,DELETE,OPTIONS" />
     <add name="Access-Control-Allow-Credentials" value="true" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Also if possible allow specific origin it resolve error explained here and How CORS works. I also find one more nicely explained article here and in SO article

also provide this parameter(if needed)

 [EnableCors(origins: "*", headers: "*", methods: "*", exposedHeaders: "X-Custom-Header")]
delta12
  • 97
  • 1
  • 8