Front-end vue + axios Access background, which has been treated with cross-domain problem (port 7777) front-end port 8080, the background, the normal interface data is sent to accept all have no problem, today the background can write file download function (returns the file), the front-end when using axios download prompt Access - Control - Allow - Origin, don't understand why the cross-domain again, don't want to use a label directly download files (I use a token, a tag cannot carry token validation) Java code cross-domain
/* * * to support cross-domain */ @ Override Protected Boolean preHandle (ServletRequest request, ServletResponse response) throws the Exception { It it=(it) request; HttpServletResponse HttpServletResponse=(HttpServletResponse) response; HttpServletResponse. SetHeader (" Access - control - Allow - Origin ", it the getHeader (" Origin ")); /* httpServletResponse. SetHeader (" Access - Control - Allow - Origin ", "*"); */ HttpServletResponse. SetHeader (" Access - Control - Allow - the Methods ", "GET, POST, OPTIONS, PUT, DELETE"); HttpServletResponse. SetHeader (" Access - Control - Allow - Headers ", it the getHeader (" Access - Control - Request - Headers ")); //would first send an option cross-domain request, here we give the option to request directly return to normal state If (it. GetMethod () equals (RequestMethod. OPTIONS. The name ())) { HttpServletResponse. SetStatus (HttpStatus. OK. The value ()); return false; } Return super. PreHandle (request, response); }
Vue code
Async getCodeList () { Enclosing axios ({ Method: "post", Url: '/code/generator? Tables, =sys_user 'ResponseType: 'blob' }). Then (res=& gt; { Let data=https://bbs.csdn.net/topics/res.data if (! Data) { Return } Let the url=window. Url. CreateObjectURL (new Blob ([data])) Let a=document. The createElement method (' a ') A.s tyle. Display='none' A.h ref=https://bbs.csdn.net/topics/url A.s etAttribute (' download ', 'excel. XLS) Document. The body. The appendChild (a) A.c lick () Window. URL. RevokeObjectURL (a.h ref) Document. Body. RemoveChild (a) }). The catch ((error)=& gt; { The console. The log (error) }) Const {data: res}=await this. $HTTP get ('/code/listData '{ Params: { Limit: this. QueryInfo. Limit, TableName: enclosing queryInfo tableName, Page: this. QueryInfo. Page } })
An error
Access to the XMLHttpRequest at http://127.0.0.1:7777/code/generator?=sys_user 'tables from origin' http://localhost:8080 'has had been blocked by CORS policy: No' Access - Control - Allow - origin 'header is present on the requested resource.
The postman tested the interface, can the normal download file
CodePudding user response:
This is cross-domain problem, refer to the following link https://blog.csdn.net/jiangyu1013/article/details/84957502