vue.js(vue-resource) ---jsonp跨域
2017-08-12 23:32
471 查看
之前的笔记说axios没有办法处理跨域问题,所以就引入了vue-resource。使用jsonp来解决跨域问题.
vue-resource的基本用法:
参考下载文档地址:https://github.com/pagekit/vue-resource/blob/develop/docs/http.md
跨域搜索360搜索案例:
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/vue-resource/vue-resource.js" ></script>
<script type="text/javascript">
window.onload = function(){
new Vue({
el:"#main",
data:{},
methods:{
sendJSONP1:function(){
//
this.$http.jsonp("https://sug.so.360.cn/suggest",{
params:{
word:'a'
}
}).then(resp=>{
console.log(resp.data.s);
},response => {
console.log("发送失败"+response.status+","+response.statusText);
});
}
}
});
}
</script>
</head>
<body>
<div id="main">
<button type="button" @click="sendJSONP1">向360搜索发送请求</button>
</div>
</body>
</html>
注:this.$http 在引入vue-resource.js 之后,在你创建vue实例的时候就会有$http属性来完成vue发送http请求的需求。具体的可以参考AIP文档。
运行结果:
vue-resource的基本用法:
get(url, [options])
head(url, [options])
delete(url, [options])
jsonp(url, [options])
post(url, [body], [options])
put(url, [body], [options])
patch(url, [body], [options])
参考下载文档地址:https://github.com/pagekit/vue-resource/blob/develop/docs/http.md
跨域搜索360搜索案例:
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/vue-resource/vue-resource.js" ></script>
<script type="text/javascript">
window.onload = function(){
new Vue({
el:"#main",
data:{},
methods:{
sendJSONP1:function(){
//
this.$http.jsonp("https://sug.so.360.cn/suggest",{
params:{
word:'a'
}
}).then(resp=>{
console.log(resp.data.s);
},response => {
console.log("发送失败"+response.status+","+response.statusText);
});
}
}
});
}
</script>
</head>
<body>
<div id="main">
<button type="button" @click="sendJSONP1">向360搜索发送请求</button>
</div>
</body>
</html>
注:this.$http 在引入vue-resource.js 之后,在你创建vue实例的时候就会有$http属性来完成vue发送http请求的需求。具体的可以参考AIP文档。
运行结果:
相关文章推荐
- 基于vue-resource jsonp跨域问题的解决方法
- require.js+vue+vue-router+vue-resource开发微信上传图片组件
- jsonp跨域js
- js实现跨域(jsonp, iframe+window.name, iframe+window.domain, iframe+window.postMessage)
- 利用jsonp跨域调用百度js实现搜索框智能提示
- vue-resource 跨域 post请求 示例
- JS JSONP跨域请求实例详解
- js原生态跨域请求与ajax中jsonp跨域请求
- js跨域总结(jsonp,postMessage,CORS)
- Vue.js——vue-resource全攻略
- vue+node.js跨域问题的解决
- 关于js跨域JSONP的使用
- JS封装Ajax插件(同域&&jsonp跨域)
- 原生js的ajax和解决跨域的jsonp(实例讲解)
- js/jquery_jsonp 跨域
- JS 跨域资源共享 -- Ajax CORS / img ping / jsonp
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- js便签笔记(13)——jsonp其实很简单【ajax跨域请求】
- js实现跨域(jsonp, iframe+window.name, iframe+window.domain, iframe+window.postMessage)
- vue-resource 1.2 jsonp用法(实现百度搜索功能)