简单请求实现

<p><span style="font-size:18px"><strong>&bull;&nbsp; 简单请求实现</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>&bull;&nbsp;&nbsp; 如果该API不支持跨域访问CORS功能, API网关直接返回报错信息,不再向后端服务器转发该API请求。</p> <p>&bull;&nbsp;&nbsp; 如果该API支持跨域访问CORS功能,后端服务器根据自身的规则返回响应信息,如果后端服务器的返回信息中没有携带相关字段,API网关会补齐相关字段。</p> <p><span style="font-size:16px"><strong>示例一:后端服务器允许任意域的访问</strong></span></p> <p>1.&nbsp; 浏览器发送携带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.&nbsp; 后端服务器返回的响应信息:</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>&nbsp;</p> <p><span style="font-family:Consolas">{&quot;ret&quot;:&quot;0&quot;, &quot;msg&quot;:&quot;succ&quot;}</span></p> </td> </tr> </tbody> </table> <p>3.&nbsp; 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>&nbsp;</p> <p><span style="font-family:Consolas">{&quot;ret&quot;:&quot;0&quot;, &quot;msg&quot;:&quot;succ&quot;}</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.&nbsp; 浏览器发送携带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.&nbsp; 后端服务器返回的响应信息:</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>&nbsp;</p> <p><span style="font-family:Consolas">{&quot;ret&quot;:&quot;0&quot;, &quot;msg&quot;:&quot;succ&quot;}</span></p> </td> </tr> </tbody> </table> <p>Access-Control-Allow-Origin:后端服务器接受&ldquo;http://www.Server-test.com&rdquo;的请求。</p> <p>3.&nbsp; &nbsp;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>&nbsp;</p> <p><span style="font-family:Consolas">{&quot;ret&quot;:&quot;0&quot;, &quot;msg&quot;:&quot;succ&quot;}</span></p> </td> </tr> </tbody> </table> <p>Access-Control-Allow-Origin:API网关接受&ldquo;http://www.Server-test.com&rdquo;的请求。</p>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询 - 7x24 小时

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

7x24 小时,急速解答

工单支持

解决云产品相关技术问题