xhr.open("PUT","http://localhost:3000/user/1")
user内容:

xhr.send(`username=hhh&password=575`)
完整代码:(只复制了body里面的代码)
- <button id="myget">getbutton>
- <button id="mypost">postbutton>
- <button id="myput">putbutton>
- <button id="mypatch">patchbutton>
- <button id="mydelete">deletebutton>
- <script>
- myput.onclick = function(){
- var xhr = new XMLHttpRequest()
- xhr.open("PUT","http://localhost:3000/user/1")
-
- xhr.onload = function(){
- if(/^2\d{2}$/.test(xhr.status)){
- console.log(JSON.parse(xhr.responseText))
- }else if(xhr.status === 404){
- console.log("页面加载错误")
- }
- }
-
- //表单格式
- xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
- xhr.send(`username=hhh&password=575`)
-
- //json格式
- // xhr.setRequestHeader("Content-Type","application/json")
- // xhr.send(JSON.stringify({
- // username:"hhh",
- // password:"575"
- // }))
- }
- script>
结果:

json原来的内容:

json文件内容的变化:

user内容变化:

xhr.open("PATCH","http://localhost:3000/user/2")
xhr.send(`username=dd`)
- <button id="myget">getbutton>
- <button id="mypost">postbutton>
- <button id="myput">putbutton>
- <button id="mypatch">patchbutton>
- <button id="mydelete">deletebutton>
- <script>
- myput.onclick = function(){
- var xhr = new XMLHttpRequest()
- xhr.open("PATCH","http://localhost:3000/user/2")
-
- xhr.onload = function(){
- if(/^2\d{2}$/.test(xhr.status)){
- console.log(JSON.parse(xhr.responseText))
- }else if(xhr.status === 404){
- console.log("页面加载错误")
- }
- }
-
- //表单格式
- xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
- xhr.send(`username=dd`)
-
- }
- script>
结果:
user本来的内容:

修改后的内容:

xhr.open("DELETE","http://localhost:3000/user/2")
- <button id="myget">getbutton>
- <button id="mypost">postbutton>
- <button id="myput">putbutton>
- <button id="mypatch">patchbutton>
- <button id="mydelete">deletebutton>
- <script>
- myput.onclick = function(){
- var xhr = new XMLHttpRequest()
- xhr.open("DELETE","http://localhost:3000/user/2")
-
- xhr.onload = function(){
- if(/^2\d{2}$/.test(xhr.status)){
- console.log(JSON.parse(xhr.responseText))
- }else if(xhr.status === 404){
- console.log("页面加载错误")
- }
- }
- xhr.send()
- }
- script>
结果:
user本来的内容:

删除后的内容:
