<p>服务器服务器不支持跨域请求</p><p>此时可以在config/index.js中的proxyTable中添加:</p><pre class="brush:java;toolbar:false">proxyTable:{ &quot;/WebService.asmx&quot;:{ target:&quot;http://localhost:12900/jsnf_service/&quot;, logLevel:&#39;debug&#39;, 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&amp;&amp;clearTimeout(this._ajaxTimer); this._ajaxTimer=setTimeout(function(){ varrequest_script=document.createElement(&#39;script&#39;); request_script.setAttribute(&quot;id&quot;,&quot;request_script&quot;); request_script.src=&#39;http://xxxx&#39;+&#39;&amp;t=&#39;+Date.now(); window.callback=function(response){ //移除脚本 document.body.removeChild(document.getElementById(&#39;request_script&#39;)); console.log(response.content); } document.body.appendChild(request_script); },500); },</pre>
返回顶部 留言