测试跨域是否正常.
DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>跨域测试title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/4.5.3/css/bootstrap.min.css" >
head>
<body>
<div class="container"><h1 style="margin-top:50px">跨域测试h1><p class="text-left">API地址:p><input type="text" style="width:600px;height:30px;font-size:14px;" id="urlText" value=""/><p> <p/><p class="text-left">Token:p><input type="text" style="width:600px;height:30px;font-size:14px;" id="tokenTxt" value=""/><p> <p/><input type="button" class="btn btn-outline-primary" id="cors" value="验证跨域"/>
div>
<script src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" >script>
<script src="https://cdn.staticfile.org/popper.js/1.8.0/popper.min.js" >script>
<script src="https://cdn.staticfile.org/bootstrap/4.5.3/js/bootstrap.min.js" >script><script type="text/javascript">$("#cors").on('click', function (event) {event.preventDefault();var url2 = $("#urlText").val();$.get({contentType: 'application/x-www-form-urlencoded;charset=UTF-8',url: url2,beforeSend: function (xhr) {if ($("#tokenTxt").val().trim()) {xhr.setRequestHeader("Authorization", "Bearer " + $("#tokenTxt").val());}},success: function (data) {alert("success");},error:function(data){alert("failed");}})});
script>
body>
html>
niginx跨域设置
这是基于宝塔的配置文件
server
{listen 8866;server_name you_server_ip;index index.php index.html index.htm default.php default.htm default.html;root /www/wwwroot/you_project_path;add_header Access-Control-Allow-Origin '*';add_header Access-Control-Allow-Methods 'POST,GET,OPTIONS';add_header Access-Control-Allow-Headers '*'; location ~ /purge(/.*) {proxy_cache_purge cache_one $host$1$is_args$args;}include /www/server/panel/vhost/nginx/proxy/you_project_path/*.conf;include enable-php-00.conf;include /www/server/panel/vhost/rewrite/you_project_path;location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md){return 404;}location ~ ^/.well-known/{allow all;}if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {return 403;}access_log /www/wwwlogs/xxx.log;error_log /www/wwwlogs/xxxx.log;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!