jsonp跨域請求,vue-resource + php前後端分裂做webapp__c#

來源:互聯網
上載者:User

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頁面;

 
 
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.