如何使用HTACCESS阻止CORS错误?
嗨,
我有这个ajax从我自己的域中请求数据:
$(document).ready(function() {
var user = $("#username").val();
var pass = $("#password").val();
var url = '/index.php?app=core&module=global§ion=login&do=process';
$.ajax({
cache: false,
type: "post",
url: url,
data: {"auth_key" : "880ea6a14ea49e853634fbdc5015a024", "referer" : "/index.php?app=core&module=global&section=login&do=process", "rememberMe" : "1", "username" : user, "password" : pass},
success:function(data){
console.log("connected");
},
error:function(data){
console.log("there was a problem");
}
});
});
但是我在控制台上遇到了此错误:
cross-origin请求阻止:相同的原点策略删除读取远程资源,网址为https://example.com/index.com/index.com/index .php?app = core& module = global& section = login& do = process。 (原因:CORS请求没有成功)。状态代码:( null)。
,
因此我将这些行添加到root上的HTACCESS中
Header set X-Content-Type-Options "nosniff"
Header set X-XSS-Protection "1; mode=block"
# Always set these headers for CORS.
Header always set Access-Control-Max-Age 1728000
Header always set Access-Control-Allow-Origin: "*"
Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header always set Access-Control-Allow-Headers: "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,C$
Header always set Access-Control-Allow-Credentials true
,但仍然不起作用。我想知道我是否把它放在错误的地方?我原始的HTACCESS文件看起来像这样:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php71"
php_value upload_max_filesize 200M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php71"
php_value upload_max_filesize 200M
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
我尝试将其添加到顶部,然后在底部添加,但没有任何可用。这里有什么交易?
谢谢。
Hi,
I have this ajax that requests data from my own domain:
$(document).ready(function() {
var user = $("#username").val();
var pass = $("#password").val();
var url = '/index.php?app=core&module=global§ion=login&do=process';
$.ajax({
cache: false,
type: "post",
url: url,
data: {"auth_key" : "880ea6a14ea49e853634fbdc5015a024", "referer" : "/index.php?app=core&module=global§ion=login&do=process", "rememberMe" : "1", "username" : user, "password" : pass},
success:function(data){
console.log("connected");
},
error:function(data){
console.log("there was a problem");
}
});
});
but I get this error on console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/index.php?app=core&module=global§ion=login&do=process. (Reason: CORS request did not succeed). Status code: (null).
so I added these lines to my htaccess on root
Header set X-Content-Type-Options "nosniff"
Header set X-XSS-Protection "1; mode=block"
# Always set these headers for CORS.
Header always set Access-Control-Max-Age 1728000
Header always set Access-Control-Allow-Origin: "*"
Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header always set Access-Control-Allow-Headers: "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,C$
Header always set Access-Control-Allow-Credentials true
and it still doesnt work. I wonder if I am putting it on the wrong place, maybe? My original htaccess file looks like this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php71"
php_value upload_max_filesize 200M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php71"
php_value upload_max_filesize 200M
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
I tried by adding it on top and then on bottom but nothing works. Whats the deal here?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论