js方法请求get
let http = new XMLHttpRequest();
http.open("get","http://jsonplaceholder.typicode.com/posts")
http.send();
http.onreadystatechange = function(){
if(http.readyState == 4 && http.status == 201){
console.log(JSON.parse(http.responseText));
}
}
jq方法请求get
$.get("http://jsonplaceholder.typicode.com/posts",{id:5},function(user){
console.log(user);
})
js方法请求post
表单格式
let http = new XMLHttpRequest()
http.open("post","http://jsonplaceholder.typicode.com/posts")
http.setRequestHeader("Content-Type","appication/x-www-form-urlencoded")
http.send(JSON.stringify("id=99"))
http.onreadystatechange = function(){
if(http.readystate == 4 && http.status == 201){
console.log(JSON.parse(http.responseText));
}
}
JSON格式
let http = new XMLHttpRequest()
http.open("post","http://jsonplaceholder.typicode.com/posts")
http.setRequestHeader("Content-Type","application/json")
http.send(JSON.stringify({
"id":99
}))
http.onreadystatechange = function(){
if(http.readystate == 4 && http.status == 201){
console.log(JSON.parse);
}
}
jq方法请求post
$.post("http://jsonplaceholder.typicode.com/posts",{
"userId":10,
"title":"hello",
"body":"你好"
},function(user){
console.log(user);
})