[英]HTTP Status response returning 200 when I have set it to 201
I'm using AJAX for a login form, upon a user supplying the correct credentials it should redirect to a landing page. 我将AJAX用于登录表单,在用户提供正确的凭据后,它应该重定向到登录页面。
This is the AJAX layout (login and register hit different functions in accounts.js): 这是AJAX布局(登录和注册hits.js中的不同功能):
login.html login.php
-> accounts.js ->
register.html register.php
In both login.php and register.php there is code that checks the success like so: 在login.php和register.php中,都有如下代码检查成功:
if($success == 1)
{
echo '$success';
$_SESSION['id'] = $id;
header(201);
exit();
}
Register returns 201 if it successful, however login always returns 200 (but does echo the successful message) 如果成功,则寄存器返回201,但是登录始终返回200(但会回显成功消息)
The JS code: JS代码:
xHRObject.open("GET", "login.php?id=" + Number(new Date) + options, true);
xHRObject.onreadystatechange = function() {
if (xHRObject.readyState == 4 && xHRObject.status == 200)
{
alert(xHRObject.status);
}
else if(xHRObject.status == 201)
{
window.location.href = 'landing.html';
}
}
header takes a string as its parameter (not integer). 标头将字符串作为其参数(非整数)。
header("HTTP/1.0 201 Created")
or use http_response_code 或使用http_response_code
http_response_code(201)
You have to output headers before any content. 您必须在任何内容之前输出标头。
Try moving header(201);
尝试移动header(201);
to before the echo
call and make sure you don't have anything else outputting even earlier. 到echo
调用之前,并确保您没有其他输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.