php 向 POST 客户端返回 HTTP 200 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12022804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:40:19  来源:igfitidea点击:

Return a HTTP 200 Code to the POST client?

phphttppost

提问by Yinan Wang

I have a server sending POST to me. I need to reply with HTTP 200 OK. Server needs kind of like a "Go Ahead!" prompt before it executes another action. It requires a HTTP 200 response.

我有一台服务器向我发送 POST。我需要回复 HTTP 200 OK。服务器需要有点像“前进!” 在执行另一个操作之前提示。它需要 HTTP 200 响应。

EDITI've tried the header(), but the server for some reason won't read it?

编辑我已经尝试了 header(),但是由于某种原因服务器不会读取它?

回答by Lix

The 200 code is a standard response to a successful request... Even echoing out an empty json string would result in a 200 OK status.

200 代码是对成功请求的标准响应......即使回显一个空的 json 字符串也会导致 200 OK 状态。

echo json_encode(array());

If all you want to do is signal to your client that some process was completed, you can just echo back a custom status message or even a blank object like I demonstrated above.

如果您想要做的只是向您的客户端发出信号,表明某个过程已完成,您可以只回显自定义状态消息,甚至是我上面演示的空白对象。

If you want to actually manually send the 200 header you can do so like this -

如果您想实际手动发送 200 标头,您可以这样做 -

header("Status: 200");

Make sure that this header is send before you have anyoutput from the server.

在您从服务器获得任何输出之前,请确保发送此标头。

回答by Zachary Canann

This function call does the job: http_response_code(200);

这个函数调用完成了这项工作: http_response_code(200);

See: http://php.net/manual/en/function.http-response-code.php

请参阅:http: //php.net/manual/en/function.http-response-code.php

This function call can be thrown anywhere in the server code -- the order of when this function is called does not seem to matter.

这个函数调用可以在服务器代码的任何地方抛出——调用这个函数的顺序似乎无关紧要。