Nginx允许跨域

解决方法
配置文件中加

location /api/ {
    default_type    application/json;
    proxy_pass http://api.baidu.com/;
}

前端请求接口/api/banner/index,请求到nginx的时候会自动转发到http://api.baidu.com/banner/index

或者

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title></title>
    </head>
    <body>

    </body>
</html>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
    $.ajax({url:"https://item5.pasawu.top/api/Banner/index",success:function(result){
           console.log(result);
        }});
</script>>

Pasa吴技术博客
请先登录后发表评论
  • latest comments
  • 总共0条评论