AJAX---POST設置請求體 與 設置請求頭


        //獲取元素對象
        const result = document.getElementById("result");
        //綁定事件
        result.addEventListener("mouseover", function(){
            //1. 創建對象
            const xhr = new XMLHttpRequest();
            //2. 初始化 設置類型與 URL
            xhr.open('POST', 'http://127.0.0.1:8000/server');
            //設置請求頭
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.setRequestHeader('name','atguigu');
            //3. 設置POST的請求體
            xhr.send('a=100&b=200&c=300');
            // xhr.send('a:100&b:200&c:300');
            // xhr.send('1233211234567');
            
            //4. 事件綁定
            xhr.onreadystatechange = function(){
                //判斷
                if(xhr.readyState === 4){
                    if(xhr.status >= 200 && xhr.status < 300){
                        //處理服務端返回的結果
                        result.innerHTML = xhr.response;
                    }
                }
            }
        });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM