简体   繁体   English

将HTTP状态响应设置为201时返回200

[英]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.

相关问题 HTTP响应状态为200,但无响应显示 - HTTP response status is 200, but no response shows 收到200状态代码而不是201 - Receiving a 200 status code instead of a 201 在mootools request.JSON中响应状态代码为http 200时调用onFailure - onFailure being called when response status code is http 200 in mootools request.JSON 当响应状态不是 200 时,为什么我无法将 .json() 方法应用于响应 object? - Why I'm not being able to apply the .json() method to a Response object when the Response status is other than 200? Laravel&Vue:Axios GET呼叫返回200状态,但无响应 - Laravel & Vue: Axios GET call returning 200 status but no response Angular 7 中的 HTTP GET 请求返回错误(状态 200) - HTTP GET Request in Angular 7 returning Error (status 200) 发送 NodeJS ExpressJS 响应 HTTP 状态码 200 + 有效载荷 - Send NodeJS ExpressJS Response HTTP Status code 200 + payload 具有“状态”的http POST请求响应:200,但对象为空 - http POST request response with “status”: 200 but with an empty object 如何在Frisby Test中验证状态码是200还是201 - How to verify if either 200 or 201 status code in Frisby Test 无法获得 HTTP 201 响应 - Can't get HTTP 201 response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM