1引入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource);
2
postGetListJson(newIndex){
let url=`http://api.v.com/index.php?r=v1/user/test`
Vue.http.get(url).then((response) => {
alert(response);
});
},
php服务端添加函数
3
public function beforeAction($action) { header('Access-Control-Allow-Origin:'.\Yii::$app->params[ 'clientDomain']); return parent::beforeAction($action); // TODO: Change the autogenerated stub }
可选 4
location /rest { add_header Access-Control-Allow-Origin "*"; ... }
其他
// global Vue object Vue.http.get('/someUrl', [data], [options]).then(successCallback, errorCallback); Vue.http.post('/someUrl', [data], [options]).then(successCallback, errorCallback); // in a Vue instance this.$http.get('/someUrl', [data], [options]).then(successCallback, errorCallback); this.$http.post('/someUrl', [data], [options]).then(successCallback, errorCallback);
new Vue({ ready() { // GET request this.$http.jsonp('/someUrl', {xxx:"xxxx"}) .then(function (response) { console.log(response.data) }, function (response) { // error callback }); } })