Solution to cross domain call of webapi


discover problems

Recently, I am working on a project. The front end is Vue, the back end is webapi, and the business is the addition, deletion, modification and query of some entities. At the beginning of the project, I predicted that there was a cross domain problem, so I also looked for the information and added the configuration information in web.config:

  <add name="Access-Control-Allow-Origin" value="*" />
  <add name="Access-Control-Allow-Headers" value="*" />
  <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />

This is one of the methods found on the Internet. You can also write cross domain feature classes by yourself. You can also refer to Microsoft library CORS. You can find a lot of information on the Internet, so we will not expand it one by one.

After this configuration, I do the get test myself. It’s OK to call with JQ. I thought that the cross domain setting has been finished. However, when Tiankeng comes, the post operation can’t be called. It returns 405 errors all day. I’m depressed. What’s the matter.


After checking the principle of cross domain call on the Internet, we found that there was an option method call before cross domain post, which was used to confirm whether to run the handshake confirmation process of cross domain post. Then continue to look down. It is found that the web API project built by vs has option processing by default, and cross domain is not allowed by default.

So I commented out these codes in web. Config

  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Finally, the cross domain post request passed the test with JQ and Axios.

Maybe, this method is not a good one, but it does solve my project problems, so write it down. If there is a better way, please ask the experts of all walks of life to give directions


The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.

Recommended Today

Using mybatis to operate single table in database

brief introduction This article mainly introduces how to use mybatis to perform single table operation on MySQL database (I also mentioned the function and specific information of mybatis download and configuration file in the previous article). The environment used is as follows:JDK version: 1.8Compiler: idea2019JDBC version: 8.0.18Mybatis version: 3.5.3 configuration file First, you need to […]