没错,这又是一篇关于POST跨域请求的文章!我只能说做后台太不容易了...刚帮一个前端同事解决了《使用fetch跨域post请求》的问题,另一个前端同事也碰到了类似的跨域请求问题,不过这次是vue了。
Coding:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>vue</title>
</head>
<body>
<!-- 视图模块 -->
<div id="view" style="text-align: center;">
<h1 id="title">{{ title }}</h1>
<p id="content">{{ content }}</p>
<input type="text" v-model='title'>
<button v-on:click="sub">submit</button>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="//cdn.bootcss.com/vue-resource/1.2.1/vue-resource.js"></script>
<script type="text/javascript">
// model
var Data={
title:'This is title.',
content:'This is content.'
}
// viewModel,用于连接view和model
new Vue({
http: {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
},
el:'#view',
data:Data,
methods:{
sub:function () {
this.$http.post('http://localhost/test', {
phone: Data.title
},{
emulateJSON: true
});
}
}
})
</script>
</body>
</html>