PHP做响应jsonp的操作。
我做跨域用到了vue-resource ,这个百度一下就能下载:
下面是HTML页面ajax:
加上 v-model=" " 之后;js方法里会检查到里面的 value 值
<script src="js/lib/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="js/lib/vue.js" type="text/javascript" charset="utf-8"></script>
<script src="js/lib/vue-resource.js" type="text/javascript" charset="utf-8"></script>
<input class="name-input" type="text" name="" value="" v-model="name"/>
<input class="password-input" type="password" name="" value="" v-model="password"/>
<span class="login-btn" @click="add()">登录</span>
<script>
var vm = new Vue({
el: ".main",
data: {
name: "",
password: ""
},
methods: {
add: function () {
this.$http.jsonp("域名/login"
, {
name: this.name,
pass: this.password
}
).then(function (res) {
if (res.data.type == "1") {
window.location.href = "cg.html";
} else {
alert("账号密码错误")
}
}, function (res) {
alert("验证失败请重新登录")
});
}
}
})
</script>
public function login(){
$callback = $_GET['callback']; //这个很重要
$name = $_GET['name'];
$pass = $_GET['pass'];
if($name=='123456' and $pass=='123456'){
$json = array('type'=>1,'name'=>$name,'pass'=>$pass); //登录值错误
} else {
$json = array('type'=>0,'name'=>$name,'pass'=>$pass); //登录值正确
}
$date = json_encode($json);
echo $callback."($date)";
}
成功后则跳转到cg.html页面;