Perfect solution to cross domain problems of API and webservice

Time:2020-2-25

In many cases of Ajax calls, the webapi interface will have cross domain problems. If the same web service wants to use ajax calls, it also needs cross domain problems. The solution is as follows:

1. IIS configuration

Open IIS and select webapi or WebService after publishing, select website and select HTTP response header to add.. The operation is as follows:

The name and value to be added are


<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="x-requested-with,Content-Type"/>
<add name="Access-Control-Allow-Origin" value="*" />

2. Tag directly in web.config


<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
      <add name="Access-Control-Allow-Headers" value="x-requested-with,Content-Type"/>
      <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Bear in mind. Only one of the two methods can be used, otherwise an error will be reported.

The above article perfectly solves the cross domain problem of API and WebService, which is all the content shared by the editor. I hope it can give you a reference, and I hope you can support developpaer more.