发出 ajax Post 请求时出现 403 Forbidden 错误 [英] 403 Forbidden error when making an ajax Post request

查看:43
本文介绍了发出 ajax Post 请求时出现 403 Forbidden 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在 localhost(wamp 服务器)上运行良好,但将其移动到共享主机时,我收到错误 403(禁止).我先试过这个:

My code is working fine on localhost(wamp server) but upon moving it to a shared hosting, I get error 403 (forbidden). I tried this first:

    $.post('login.php?login', { user_email: user_email, user_password: user_password, user_remember: user_remember }, function(data)
    {
     // Some code here
    }

然后我尝试了这个:

var http = new XMLHttpRequest();
var url = "login.php?login";
var params = "user_email="+user_email+"&user_password="+user_password+"&user_remember="+user_remember;
http.open("POST", url, true);

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        // Some code here
    }
}
http.send(params);

但我仍然收到此错误:

POST http://domain_name/login.php?login 403(禁止)

POST http://domain_name/login.php?login 403 (Forbidden)

知道出了什么问题吗?

推荐答案

这里有几种可能性.

  1. 共享主机上的 403 通常意味着您尝试联系的文件存在权限问题.基本上,Web 服务器会说,不,我不会允许您访问或运行 login.php."您可能需要仔细检查该文件的所有者和权限,以防它以某种方式错误上传.

  1. A 403 on shared hosting USUALLY means that there's a permissions issue on the file you're trying to contact. Basically, the web server is saying, "No, I'm not going to allow you to access or run login.php." You might want to double-check the owner and permissions of that file, in case it was uploaded incorrectly somehow.

在某些情况下,您也有可能有一个 .htaccess 文件,该文件设置为以 403 错误代码响应,因此,如果您有其中之一,请查找有关允许/的任何内容拒绝/授予,然后尝试注释掉这些行(暂时,只是看看是否是原因)并再次尝试访问 login.php.

There's also a slim chance that you might have an .htaccess file that is set up to respond with a 403 error code under certain circumstances, so if you have one of those, look for anything that talks about allow/deny/grant, then try commenting out those lines (temporarily, just to see if it's the cause) and try to access login.php again.

另一种可能是您的 login.php 本身被编写为在某些情况下响应 403 错误代码.例如,如果您没有向它发送适当的数据,或者由于某种原因它无法验证您的登录,则它可能会以 403 HTTP 状态代码进行响应.在这种情况下,我会仔细检查以确保发送到 login.php 的数据是有效的,并且在共享服务器上正确设置了 login.php(例如检查数据库访问等)

Next possibility is that your login.php itself is written to respond with a 403 error code under certain circumstances. For example, maybe if you don't send the appropriate data to it or if it can't validate your login for some reason, maybe it responds with a 403 HTTP status code. In that scenario, I'd double-check to make sure that the data being sent to the login.php is valid and that login.php is set up correctly on the shared server (e.g. check database access, etc)

最后一种可能是您的原始文件与您的 login.php 不在同一个域中.换句话说,您正在使用 http://domainA.com/page.html 并制作 AJAX调用 http://domainB.com/login.php.这称为跨域调用,也可能导致 403,因为出于安全原因禁止跨域调用,除非您采取措施启用它们(您通常必须控制双方才能采取这些步骤).因此,如果这就是您执行 AJAX 调用的方式,请尝试将原始文件 (page.html) 复制到 login.php 所在的 domainB.com,然后从那里开始尝试.

Final possibility is that your originating file is not on the same domain as your login.php. In other words, you're on http://domainA.com/page.html and making an AJAX call to http://domainB.com/login.php. This is referred to as a cross-domain call, and can also result in a 403 because cross-domain calls are forbidden for security reasons unless you take steps to enable them (you usually have to be in control of both sides to take those steps). So if that's how you're executing the AJAX call, try copying the originating file (page.html) to domainB.com where login.php is, and try it from there.

这篇关于发出 ajax Post 请求时出现 403 Forbidden 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆