Prepare to design a Restful Api based on Nginx. You need to use DELETE and PUT request methods, and support cross-domain access. Currently, there are local virtual hostshttp://api.zlzkj.com
andhttp://127.0.0.1/api/web
Two test domains.
nginx.conf related cross-domain configuration
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
ajax request
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
Resetful Api设计
<script src="http://c.csdnimg.cn/public/common/libs/jquery/jquery-1.11.1.min.js"></script>
<script>
$.ajax({
url: 'http://api.zlzkj.com/admins/1',
type: 'DELETE',
dataType: 'JSON'
});
</script>
</body>
</html>
Access under http://api.zlzkj.com/
http://api.zlzkj.com/admins/1
You can use the DELETE request method normally
When you visit http://api.zlzkj.com/admins/1
under http://127.0.0.1/api/web/
, you will find that the Request Method is filtered into OPTINOS mode, normally it should be DELETE mode, which caused the server's 405 Method Not Allowed
I have also found some related articles over the wall. It seems that after their Nginx is configured in this way, the DELETE request method can be used normally across domains. However, on my side, it can only be used in the same domain, and the Request Method will be filtered across domains. In OPTINOS mode, a 405 error occurs.
Is it a problem with the Nginx version? Environment configuration problem? I hope you can give me some insights, thank you.
OPTIONS
请求比较特殊,该方法用于请求服务器告知其支持哪些其他的功能和方法。在跨域的时候,浏览器会自动发起一个
OPTIONS
请求。当你的服务器响应了
OPTIONS
When requesting, there will be a response similar to the following:If your server does not handle the response
OPTIONS
, there will be a response like this:It can be seen that the
In the configuration, add the following configuration: 🎜Allow
response header is missingAllow
响应头所以,你应该有处理这个
OPTIONS
So, you should have a service to handle thisrequest. This can be done directly with nginx.