<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div id="app">
<div class="container">
<div class="row">
<h3> 静态请求【 模拟数据 】 </h3>
<button type="button" class="btn btn-primary" @click = "getStatic" > getJson </button>
<h3> 动态请求 【 真实接口】 </h3>
<button type="button" class="btn btn-primary" @click = "get"> get </button>
<button type="button" class="btn btn-primary" @click = "post"> post </button>
</div>
</div>
</div>
new Vue({
el: '#app',
methods: {
getStatic () {
fetch('./mock/data/data.json')
.then( res => res.json() )
.then( data => console.log( data ))
.catch( error => console.log( error ))
},
get () {
fetch('http://localhost/get.php?a=1&b=2')
.then( res => res.text() )
.then( data => console.log( data ))
.catch( error => console.log( error ))
},
post () {
fetch('http://localhost/post.php', {
method: 'POST',
body: new URLSearchParams([['a',1],['b',2]]).toString(),
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
})
}).then(res => res.text())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
}
}
})