同服務(wù)器,其他接口正常。唯獨(dú)上傳圖片接口報(bào)跨域錯(cuò)誤。代碼如下,求教大佬,這代碼有無(wú)問(wèn)題?或者是 php 后臺(tái)需要修改?
<!DOCTYPE html>
<html lang="en" style="font-size: 15px;">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>圖片</title>
<link rel="stylesheet" href="css/base.css" type="text/css">
<link rel="stylesheet" href="css/layer.css" type="text/css">
<script src="lib/vue-2.5.1.js" type="text/javascript"></script>
<script src="lib/vue-resource.js" type="text/javascript"></script>
<script src="lib/jquery.min.js" type="text/javascript"></script>
<script src="lib/layer.js" type="text/javascript"></script>
<style>
html,
body {
width: 100%;
height: 100%;
}
#box {
width: 50%;
height: 11.521rem;
margin: 2.88rem auto;
border: 1px solid black;
}
.imgbox {
width: 100%;
height: 5.76rem;
border: 1px solid red;
background: url();
}
</style>
</head>
<body>
<div id="box">
<div :style="'background:url('+imgurl+');'" class="imgbox"></div>
<input id="imginput" class="imginput" type="file" accept="images/*" @change="upfileimg">
</div>
</body>
<script>
var vm = new Vue({
el:"#box",
data:{
imgurl:"",
imagename:""
},
methods:{
upfileimg:function (argument) {
var files = argument.target.files[0];
if (!files.type.match('image.*')) {
layer.open({
content:"不是圖片格式!",
skin:"msg"
})
return;
}
this.imagename = files.name;
var self = this;
var regead = new FileReader();
regead.onload = function (e) {
self.imgurl = e.target.result;
};
regead.readAsDataURL(files);
// var imageurl = document.getElementById('imginput').files[0];
// console.log(imageurl); //這個(gè)imageurl有說(shuō)法可以替換下面的this.imageurl,但還是不行
var fd = new FormData();
fd.append("file",this.imgurl,this.imagename);
this.$http.post("xxx",{
file:fd
}).then(function (argument) {
console.log(argument);
},function (argument) {
console.log(argument);
})
}
}
})
</script>
</html>
php 單獨(dú)出一個(gè)接 base64 的接口,普通的 post 就可以上傳 base64.
為何這個(gè) base64 轉(zhuǎn)二進(jìn)制不行?后臺(tái)好像用的 thinkphp 框架
你這個(gè)是屬于跨域不被允許的問(wèn)題,我也遇到過(guò)。
當(dāng)時(shí)我的解決方法是,在config/index.js里面的proxyTable{} 設(shè)置代理解決的。
下面是我設(shè)置的三個(gè)代理你可以參考一下:
proxyTable: {
//獲取單首歌曲信息
'/getsongapi': {
target: 'http://www.kugou.com/',//請(qǐng)求數(shù)據(jù)的地址
secure: false,//false:請(qǐng)求方式為http,true:請(qǐng)求方式為https
changeOrigin: true,//是否進(jìn)行跨域
pathRewrite: {
'^/getsongapi': '/' //target后面的文件夾名字
},
},
//從m.kugou.com獲取數(shù)據(jù)
'/api': {
target: 'http://m.kugou.com/', //請(qǐng)求數(shù)據(jù)的地址
secure: false, //false:請(qǐng)求方式為http,true:請(qǐng)求方式為https
changeOrigin: true, //是否進(jìn)行跨域
pathRewrite: {
'^/api': '/' //target后面的文件夾名字
},
},
'/search': {
target: 'http://mobilecdn.kugou.com/', //請(qǐng)求數(shù)據(jù)的地址
changeOrigin: true,
pathRewrite: {
'^/search': '/' //target后面的文件夾名字
}
},
headers: {
'User-Agent': 'Mozilla / 5.0(Linux; Android 6.0; Nexus 5 Build / MRA58N) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 55.0 .2883 .87 Mobile Safari / 537.36'
}
}
報(bào)這個(gè)錯(cuò)一般需要后端去解決下,也很簡(jiǎn)單。
請(qǐng)求分為兩種,簡(jiǎn)單請(qǐng)求和非簡(jiǎn)單請(qǐng)求
雖然這里和其它的地方都是使用post請(qǐng)求,但是可能你設(shè)置了HTTP的頭信息使之成為了跨域非簡(jiǎn)請(qǐng)求
跨域資源共享 CORS 詳解
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。