简单请求实现
<p><span style="font-size:18px"><strong>• 简单请求实现</strong></span></p>
<p>平安云API网关默认不支持跨域访问CORS功能,如要需要支持跨域访问CORS,请先在<a href="https://pinganyun.com/console/apiGateway" target="_blank">API网关管理控制台</a>开启CORS并重新发布API。</p>
<p>当API网关接收到某个API的简单跨域请求时:</p>
<p>• 如果该API不支持跨域访问CORS功能, API网关直接返回报错信息,不再向后端服务器转发该API请求。</p>
<p>• 如果该API支持跨域访问CORS功能,后端服务器根据自身的规则返回响应信息,如果后端服务器的返回信息中没有携带相关字段,API网关会补齐相关字段。</p>
<p><span style="font-size:16px"><strong>示例一:后端服务器允许任意域的访问</strong></span></p>
<p>1. 浏览器发送携带Origin的API请求:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">GET /testSimpleCors HTTP/1.1</span></p>
<p><span style="font-family:Consolas">Host: pinganyun.com</span></p>
<p><span style="font-family:Consolas">Orgin: http://www.Server-test.com</span></p>
<p><span style="font-family:Consolas">Content-Type: application/x-www-form-urlencoded; charset=utf-8</span></p>
<p><span style="font-family:Consolas">Accept: application/json</span></p>
<p><span style="font-family:Consolas">Date: Tue, 21 Jan 2019 21:15:12 GMT</span></p>
</td>
</tr>
</tbody>
</table>
<p>Origin:表示请求的来源。后端服务器会根据Origin的取值,判断是否同意本次请求。如果后端服务器返回的消息中没有携带Access-Control-Allow-Origin,那么API网关就会添加Access-Control-Allow-Origin: *</p>
<p>2. 后端服务器返回的响应信息:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">HTTP/1.1 200 OK</span></p>
<p><span style="font-family:Consolas">Date: Tue, 21 Jan 2019 21:15:13 GMT</span></p>
<p><span style="font-family:Consolas">Content-Type: application/json</span></p>
<p><span style="font-family:Consolas">Content-Length: 25</span></p>
<p> </p>
<p><span style="font-family:Consolas">{"ret":"0", "msg":"succ"}</span></p>
</td>
</tr>
</tbody>
</table>
<p>3. API网关发送给浏览器的响应信息:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">HTTP/1.1 200 OK</span></p>
<p><span style="font-family:Consolas">Date: Tue, 21 Jan 2019 21:15:13 GMT</span></p>
<p><span style="font-family:Consolas">Content-Type: application/json</span></p>
<p><span style="font-family:Consolas">Content-Length: 25</span></p>
<p><span style="font-family:Consolas">Access-Control-Allow-Origin: *</span></p>
<p><span style="font-family:Consolas">Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers</span></p>
<p> </p>
<p><span style="font-family:Consolas">{"ret":"0", "msg":"succ"}</span></p>
</td>
</tr>
</tbody>
</table>
<p>Access-Control-Allow-Origin:取值为*,表示API网关接受任意域的请求。</p>
<p><span style="font-size:16px"><strong>实例二:后端服务器允许指定域的访问</strong></span></p>
<p>1. 浏览器发送携带Origin的API请求:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">GET /testSimpleCors HTTP/1.1</span></p>
<p><span style="font-family:Consolas">Host: pinganyun.com</span></p>
<p><span style="font-family:Consolas">Orgin: http://www.Server-test.com</span></p>
<p><span style="font-family:Consolas">Content-Type: application/x-www-form-urlencoded; charset=utf-8</span></p>
<p><span style="font-family:Consolas">Accept: application/json</span></p>
<p><span style="font-family:Consolas">Date: Tue, 21 Jan 2019 21:15:12 GMT</span></p>
</td>
</tr>
</tbody>
</table>
<p>Origin:表示请求的来源。后端服务器根据Origin的取值,判断是否同意本次请求。</p>
<p>2. 后端服务器返回的响应信息:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">HTTP/1.1 200 OK</span></p>
<p><span style="font-family:Consolas">Date: Tue, 21 Jan 2019 21:15:13 GMT</span></p>
<p><span style="font-family:Consolas">Content-Type: application/json</span></p>
<p><span style="font-family:Consolas">Content-Length: 25</span></p>
<p><span style="font-family:Consolas">Access-Control-Allow-Origin: http://www.Server-test.com</span></p>
<p> </p>
<p><span style="font-family:Consolas">{"ret":"0", "msg":"succ"}</span></p>
</td>
</tr>
</tbody>
</table>
<p>Access-Control-Allow-Origin:后端服务器接受“http://www.Server-test.com”的请求。</p>
<p>3. API网关发送给浏览器的响应信息:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">HTTP/1.1 200 OK</span></p>
<p><span style="font-family:Consolas">Date: Tue, 21 Jan 2019 21:15:13 GMT</span></p>
<p><span style="font-family:Consolas">Content-Type: application/json</span></p>
<p><span style="font-family:Consolas">Content-Length: 25</span></p>
<p><span style="font-family:Consolas">Access-Control-Allow-Origin: http://www.Server-test.com</span></p>
<p><span style="font-family:Consolas">Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers</span></p>
<p> </p>
<p><span style="font-family:Consolas">{"ret":"0", "msg":"succ"}</span></p>
</td>
</tr>
</tbody>
</table>
<p>Access-Control-Allow-Origin:API网关接受“http://www.Server-test.com”的请求。</p>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!