久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='oXzao'><tr id='oXzao'><dt id='oXzao'><q id='oXzao'><span id='oXzao'><b id='oXzao'><form id='oXzao'><ins id='oXzao'></ins><ul id='oXzao'></ul><sub id='oXzao'></sub></form><legend id='oXzao'></legend><bdo id='oXzao'><pre id='oXzao'><center id='oXzao'></center></pre></bdo></b><th id='oXzao'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='oXzao'><tfoot id='oXzao'></tfoot><dl id='oXzao'><fieldset id='oXzao'></fieldset></dl></div>
<legend id='oXzao'><style id='oXzao'><dir id='oXzao'><q id='oXzao'></q></dir></style></legend>
  1. <tfoot id='oXzao'></tfoot>
      <bdo id='oXzao'></bdo><ul id='oXzao'></ul>

      <small id='oXzao'></small><noframes id='oXzao'>

      POST 適用于 JQuery,但不適用于 XMLHttpRequest

      POST works with JQuery but does not work with XMLHttpRequest(POST 適用于 JQuery,但不適用于 XMLHttpRequest)
        <tfoot id='q6eur'></tfoot>

        <small id='q6eur'></small><noframes id='q6eur'>

          <legend id='q6eur'><style id='q6eur'><dir id='q6eur'><q id='q6eur'></q></dir></style></legend>
            <tbody id='q6eur'></tbody>

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

                本文介紹了POST 適用于 JQuery,但不適用于 XMLHttpRequest的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                所以我正在嘗試從 Javascript 發布到我的服務器 (php),并且嘗試不使用 JQuery.

                So I'm trying to POST to my server (php) from Javascript and am trying to not use JQuery.

                此代碼工作并將必要的數據發布到數據庫

                This code works and posts the necessary data to the database

                var msg = {};
                msg['name'] = 'joe';
                msg['message'] = 'why no work';
                
                $.post(phpURL, msg, function(data) {});
                

                但這個沒有

                var xhr = new XMLHttpRequest();
                xhr.open("POST", phpURL, true);
                xhr.send(msg);
                

                我什至查看了我的 php 日志,查看了標題,我能看到的 JQuery 與 XHR 的唯一區別是內容類型標題 "application/x-www-form-urlencoded;charset=UTF-8" 和這個頭 "x-requested-with"XMLHttpRequest".

                I even looked at my php logs, looked at the headers and the only difference of the JQuery one from the XHR one I could see was the content-type header "application/x-www-form-urlencoded; charset=UTF-8" and this header "x-requested-with" "XMLHttpRequest".

                所以我嘗試了以下標題的所有組合.

                So I tried all combinations of the following headers.

                var xhr = new XMLHttpRequest();
                xhr.open("POST", phpURL, true);
                //xhr.setRequestHeader('Content-Type', 'application/json');
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                //xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
                xhr.send(msg);
                

                沒有效果.

                值得一提的是,如果我嘗試在任何地方添加 JSON.stringify(msg),它不起作用,無論是在 JQuery 還是 XHR 中.但我想先讓這個工作,并解釋這個奇怪的差異.

                It is worth mentioning if I try to add JSON.stringify(msg) anywhere, it does not work, neither in JQuery or XHR. But I would like to get this working first, and explain this bizarre difference.

                我傾向于認為這是一個 Javascript 問題,因為 JQuery 帖子有效,此外,服務器的 GET 請求和我嘗試發布到的同一個表也有效.

                I am inclined to believe this is a Javascript issue since the JQuery post works and in addition, a GET request of the server and the same table I'm trying to post to does work.

                推薦答案

                不要將 JavaScript 對象與 JSON 混淆.

                如果您將對象傳遞給 jQuery 的 data 參數,那么它會將其編碼為 application/x-www-form-urlencoded 數據(不是 JSON!).

                If you pass an object to jQuery's data parameter then it will encode it as application/x-www-form-urlencoded data (not JSON!).

                如果您將 application/x-www-form-urlencoded 數據發布到 PHP,那么它將解析它并用它填充 $_POST 超全局.

                If you POST application/x-www-form-urlencoded data to PHP then it will parse it and populate the $_POST superglobal with it.

                如果您將對象傳遞給 XMLHttpRequest 對象的 send() 方法,那么它將為您編碼.它會隱式調用 .toString() 并發送任何有用的東西.

                If you pass an object to the send() method of an XMLHttpRequest object then it will not encode it for you. It will invoke .toString() on it implicit and send nothing very useful at all.

                要達到與 jQuery 相同的效果,您需要 自己編碼數據.不要忘記同時設置 Content-Type 標頭!

                To achieve the same effect as jQuery will do then you need to encode the data yourself. Don't forget to also set the Content-Type header!

                const encoded = new URLSearchParams(Object.entries(msg)).toString();
                const xhr = new XMLHttpRequest();
                xhr.open("POST", phpURL, true);
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send(encoded);
                


                如果您想發送 JSON,那么您還必須對其進行編碼,但這只需使用 JSON.stringify() 雖然您還需要設置 Content-Type 標頭(到 application/json 這次).

                const encoded = JSON.stringify(msg);
                const xhr = new XMLHttpRequest();
                xhr.open("POST", phpURL, true);
                xhr.setRequestHeader("Content-Type", "application/json");
                xhr.send(encoded);
                

                但是,PHP 不會自動解析 JSON,因此 $_POST 將保持為空,因此您 需要手動解析.

                However, PHP will not parse JSON automatically so $_POST will remain empty, so you need to parse it manually.

                <?php 
                    $json = file_get_contents('php://input');
                    $msg = json_decode($json);
                

                這篇關于POST 適用于 JQuery,但不適用于 XMLHttpRequest的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)

              • <i id='iNTyE'><tr id='iNTyE'><dt id='iNTyE'><q id='iNTyE'><span id='iNTyE'><b id='iNTyE'><form id='iNTyE'><ins id='iNTyE'></ins><ul id='iNTyE'></ul><sub id='iNTyE'></sub></form><legend id='iNTyE'></legend><bdo id='iNTyE'><pre id='iNTyE'><center id='iNTyE'></center></pre></bdo></b><th id='iNTyE'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='iNTyE'><tfoot id='iNTyE'></tfoot><dl id='iNTyE'><fieldset id='iNTyE'></fieldset></dl></div>
                <legend id='iNTyE'><style id='iNTyE'><dir id='iNTyE'><q id='iNTyE'></q></dir></style></legend>
                  <tbody id='iNTyE'></tbody>

                  <small id='iNTyE'></small><noframes id='iNTyE'>

                        <tfoot id='iNTyE'></tfoot>
                          <bdo id='iNTyE'></bdo><ul id='iNTyE'></ul>
                        • 主站蜘蛛池模板: 欧美黄页 | 日本在线观看视频 | 四虎影院新地址 | 99国内精品久久久久久久 | 欧美国产精品 | 国际精品鲁一鲁一区二区小说 | 国产三级网站 | 国产伦精品一区二区三区精品视频 | 久久精品免费一区二区三 | 欧美综合久久久 | 天天爽天天操 | 久久久久久久久久久一区二区 | 国产精品久久久久久亚洲调教 | 亚洲综合一区二区三区 | 欧美aaaa视频 | 国产精品美女久久久免费 | 国产一区二区三区在线免费 | 久久久国产一区二区三区 | 免费av播放 | 美女三区 | 色综合99| 欧美一级全黄 | 日韩精品在线观看一区二区 | 久久国产精品一区二区三区 | 亚洲激情一区二区 | 日本免费在线观看视频 | 欧美亚洲第一区 | 国产电影一区二区在线观看 | 亚洲三区在线 | 日韩欧美中文字幕在线观看 | 一区二区在线不卡 | 亚洲网站在线 | 西西裸体做爰视频 | 天天玩天天操天天干 | 精品自拍视频在线观看 | 午夜精品在线观看 | 欧美黑人一级爽快片淫片高清 | 亚洲精品电影网在线观看 | 99综合| 亚洲视频三 | 欧美成人激情 |