• <small id='SKExm'></small><noframes id='SKExm'>

    <i id='SKExm'><tr id='SKExm'><dt id='SKExm'><q id='SKExm'><span id='SKExm'><b id='SKExm'><form id='SKExm'><ins id='SKExm'></ins><ul id='SKExm'></ul><sub id='SKExm'></sub></form><legend id='SKExm'></legend><bdo id='SKExm'><pre id='SKExm'><center id='SKExm'></center></pre></bdo></b><th id='SKExm'></th></span></q></dt></tr></i><div id='SKExm'><tfoot id='SKExm'></tfoot><dl id='SKExm'><fieldset id='SKExm'></fieldset></dl></div>

    1. <legend id='SKExm'><style id='SKExm'><dir id='SKExm'><q id='SKExm'></q></dir></style></legend><tfoot id='SKExm'></tfoot>
        <bdo id='SKExm'></bdo><ul id='SKExm'></ul>

        将 JSON 发送到服务器并返回 JSON,无需 JQuery

        时间:2023-10-14

        <i id='TIoVp'><tr id='TIoVp'><dt id='TIoVp'><q id='TIoVp'><span id='TIoVp'><b id='TIoVp'><form id='TIoVp'><ins id='TIoVp'></ins><ul id='TIoVp'></ul><sub id='TIoVp'></sub></form><legend id='TIoVp'></legend><bdo id='TIoVp'><pre id='TIoVp'><center id='TIoVp'></center></pre></bdo></b><th id='TIoVp'></th></span></q></dt></tr></i><div id='TIoVp'><tfoot id='TIoVp'></tfoot><dl id='TIoVp'><fieldset id='TIoVp'></fieldset></dl></div>
              <tbody id='TIoVp'></tbody>

              <bdo id='TIoVp'></bdo><ul id='TIoVp'></ul>
              • <small id='TIoVp'></small><noframes id='TIoVp'>

                <tfoot id='TIoVp'></tfoot>
                <legend id='TIoVp'><style id='TIoVp'><dir id='TIoVp'><q id='TIoVp'></q></dir></style></legend>
                1. 本文介绍了将 JSON 发送到服务器并返回 JSON,无需 JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要向服务器发送一个 JSON(我可以对其进行字符串化)并在用户端检索生成的 JSON,而不使用 JQuery.

                  I need to send a JSON (which I can stringify) to the server and to retrieve the resulting JSON on the user side, without using JQuery.

                  如果我应该使用 GET,我如何将 JSON 作为参数传递?会不会有太长的风险?

                  If I should use a GET, how do I pass the JSON as a parameter? Is there a risk it would be too long?

                  如果我应该使用 POST,如何在 GET 中设置等效的 onload 函数?

                  If I should use a POST, how do I set the equivalent of an onload function in GET?

                  或者我应该使用其他方法吗?

                  Or should I use a different method?

                  备注

                  这个问题不是关于发送一个简单的 AJAX.它不应该作为重复关闭.

                  This question is not about sending a simple AJAX. It should not be closed as duplicate.

                  推荐答案

                  使用POST方式发送和接收JSON格式数据

                  // Sending and receiving data in JSON format using POST method
                  //
                  var xhr = new XMLHttpRequest();
                  var url = "url";
                  xhr.open("POST", url, true);
                  xhr.setRequestHeader("Content-Type", "application/json");
                  xhr.onreadystatechange = function () {
                      if (xhr.readyState === 4 && xhr.status === 200) {
                          var json = JSON.parse(xhr.responseText);
                          console.log(json.email + ", " + json.password);
                      }
                  };
                  var data = JSON.stringify({"email": "hey@mail.com", "password": "101010"});
                  xhr.send(data);
                  

                  使用 GET 方法发送和接收 JSON 格式的数据

                  // Sending a receiving data in JSON format using GET method
                  //      
                  var xhr = new XMLHttpRequest();
                  var url = "url?data=" + encodeURIComponent(JSON.stringify({"email": "hey@mail.com", "password": "101010"}));
                  xhr.open("GET", url, true);
                  xhr.setRequestHeader("Content-Type", "application/json");
                  xhr.onreadystatechange = function () {
                      if (xhr.readyState === 4 && xhr.status === 200) {
                          var json = JSON.parse(xhr.responseText);
                          console.log(json.email + ", " + json.password);
                      }
                  };
                  xhr.send();
                  

                  使用 PHP 在服务器端处理 JSON 格式的数据

                  <?php
                  // Handling data in JSON format on the server-side using PHP
                  //
                  header("Content-Type: application/json");
                  // build a PHP variable from JSON sent using POST method
                  $v = json_decode(stripslashes(file_get_contents("php://input")));
                  // build a PHP variable from JSON sent using GET method
                  $v = json_decode(stripslashes($_GET["data"]));
                  // encode the PHP variable to JSON and send it back on client-side
                  echo json_encode($v);
                  ?>
                  

                  HTTP Get 请求的长度限制取决于所使用的服务器和客户端(浏览器),从 2kB 到 8kB.如果 URI 比服务器可以处理的长,服务器应该返回 414(Request-URI Too Long)状态.

                  The limit of the length of an HTTP Get request is dependent on both the server and the client (browser) used, from 2kB - 8kB. The server should return 414 (Request-URI Too Long) status if an URI is longer than the server can handle.

                  注意 有人说我可以用状态名代替状态值;换句话说,我可以使用 xhr.readyState === xhr.DONE 而不是 xhr.readyState === 4 问题是 Internet Explorer 使用不同的状态名称,所以它是更好地使用状态值.

                  Note Someone said that I could use state names instead of state values; in other words I could use xhr.readyState === xhr.DONE instead of xhr.readyState === 4 The problem is that Internet Explorer uses different state names so it's better to use state values.

                  这篇关于将 JSON 发送到服务器并返回 JSON,无需 JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:HTTP 状态码 0 是否有任何意义? 下一篇:XMLHttpRequest 状态 0(responseText 为空)

                  相关文章

                  最新文章

                2. <i id='JI4G6'><tr id='JI4G6'><dt id='JI4G6'><q id='JI4G6'><span id='JI4G6'><b id='JI4G6'><form id='JI4G6'><ins id='JI4G6'></ins><ul id='JI4G6'></ul><sub id='JI4G6'></sub></form><legend id='JI4G6'></legend><bdo id='JI4G6'><pre id='JI4G6'><center id='JI4G6'></center></pre></bdo></b><th id='JI4G6'></th></span></q></dt></tr></i><div id='JI4G6'><tfoot id='JI4G6'></tfoot><dl id='JI4G6'><fieldset id='JI4G6'></fieldset></dl></div>
                  <tfoot id='JI4G6'></tfoot>

                    <bdo id='JI4G6'></bdo><ul id='JI4G6'></ul>
                3. <legend id='JI4G6'><style id='JI4G6'><dir id='JI4G6'><q id='JI4G6'></q></dir></style></legend>

                    1. <small id='JI4G6'></small><noframes id='JI4G6'>