<p>服务器服务器不支持跨域请求</p><p>此时可以在config/index.js中的proxyTable中添加:</p><pre class="brush:java;toolbar:false">proxyTable:{
"/WebService.asmx":{
target:"http://localhost:12900/jsnf_service/",
logLevel:'debug',
changeOrigin:true
}
},</pre><p>这样就可以正常使用axios进行ajax请求了,但这样只能在开发模式下才能使用。打包部署的时候建议使用nginx做代理</p><p>服务器端支持跨域</p><p>如果能支持跨域那就可以使用jsonp来请求了。jsonp实际上就是通过添加script标签来添加的,请求回来的数据也是js格式。axios目前还不支持,只能自己手动创建script标签,把请求的地址赋给script标签的src属性,最后添加到head标签上,等到请求返回再把标签删掉:</p><pre class="brush:java;toolbar:false">jsonpRequest:function(a,e){
this._ajaxTimer&&clearTimeout(this._ajaxTimer);
this._ajaxTimer=setTimeout(function(){
varrequest_script=document.createElement('script');
request_script.setAttribute("id","request_script");
request_script.src='http://xxxx'+'&t='+Date.now();
window.callback=function(response){
//移除脚本
document.body.removeChild(document.getElementById('request_script'));
console.log(response.content);
}
document.body.appendChild(request_script);
},500);
},</pre>