我正在尝试从客户端向服务器发送一个 JSON 对象并接收响应,但我发现的所有方法要么不充分,要么矫枉过正
I'm trying to send a JSON object from client to server and receive a response but all the approaches I find are either inadequate or overkill
我不确定这样做的正确方法是什么。我正在尝试获取一个大的 json 文件,将其发送到服务器,对其进行处理和重新排序,然后将其发送回客户端。我不想在数据库中存储任何数据。我知道有 HTTP GET 动词,但我要输入的数据量会比最大长度 URI 长。我还读到您也不应该尝试使用 HTTP POST 来执行此操作。
我也研究了 WebSockets,但对我来说它似乎有点矫枉过正。我只需要在计算所需的时间内使用套接字,然后我会关闭它。我也想只与发送给我的客户共享数据。
有人对做什么有建议吗?也许只是通过一些我可以阅读的链接朝着正确的方向前进。我真的在寻找在这两种方法中间运行的东西。
为什么不直接使用 HTTP POST 请求?取自
上的信息框
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
> Request has body Yes
> Successful response has body Yes
> Safe No
> Idempotent No
> Cacheable Only if freshness information is included
> Allowed in HTML forms Yes
如您所见,HTTP POST 请求用于向服务器发送数据,如果 POST 请求成功,服务器会将数据发送回客户端。我认为非常适合您的情况。
POST 请求不必在 HTML 表单中使用;您可以使用 XHR、AJAX、获取 API 或您可以找到的任何其他方式向服务器发送 POST 请求。是的,您可以用它发送 JSON 数据。
如果您需要更有说服力:
When the POST request is sent via a method other than an HTML form — like via an XMLHttpRequest — the body can take any type. As described in the HTTP 1.1 specification, POST is designed to allow a uniform method to cover the following functions:
- Annotation of existing resources
- Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;
- Adding a new user through a signup modal;
- Providing a block of data, such as the result of submitting a form, to a data-handling process;
- Extending a database through an append operation.
请注意,它说 POST 请求可用于向数据处理进程提供数据块。
希望对您有所帮助。 :)
我不确定这样做的正确方法是什么。我正在尝试获取一个大的 json 文件,将其发送到服务器,对其进行处理和重新排序,然后将其发送回客户端。我不想在数据库中存储任何数据。我知道有 HTTP GET 动词,但我要输入的数据量会比最大长度 URI 长。我还读到您也不应该尝试使用 HTTP POST 来执行此操作。
我也研究了 WebSockets,但对我来说它似乎有点矫枉过正。我只需要在计算所需的时间内使用套接字,然后我会关闭它。我也想只与发送给我的客户共享数据。
有人对做什么有建议吗?也许只是通过一些我可以阅读的链接朝着正确的方向前进。我真的在寻找在这两种方法中间运行的东西。
为什么不直接使用 HTTP POST 请求?取自
上的信息框
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
> Request has body Yes
> Successful response has body Yes
> Safe No
> Idempotent No
> Cacheable Only if freshness information is included
> Allowed in HTML forms Yes
如您所见,HTTP POST 请求用于向服务器发送数据,如果 POST 请求成功,服务器会将数据发送回客户端。我认为非常适合您的情况。
POST 请求不必在 HTML 表单中使用;您可以使用 XHR、AJAX、获取 API 或您可以找到的任何其他方式向服务器发送 POST 请求。是的,您可以用它发送 JSON 数据。
如果您需要更有说服力:
When the POST request is sent via a method other than an HTML form — like via an XMLHttpRequest — the body can take any type. As described in the HTTP 1.1 specification, POST is designed to allow a uniform method to cover the following functions:
- Annotation of existing resources
- Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;
- Adding a new user through a signup modal;
- Providing a block of data, such as the result of submitting a form, to a data-handling process;
- Extending a database through an append operation.
请注意,它说 POST 请求可用于向数据处理进程提供数据块。
希望对您有所帮助。 :)