Home > Back-end >  Java vue cross-domain problem
Java vue cross-domain problem

Time:10-19

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

CodePudding user response:

reference 1st floor qybao response:
this is cross-domain problem, refer to the following link
https://blog.csdn.net/jiangyu1013/article/details/84957502

Cross domain I have solved the problem, I don't tip cross-domain request data is normal, is to download file prompt the cross-domain

CodePudding user response:

This error is the problem of cross-domain request,
You understand the inner mechanism of CORS well,
https://www.ruanyifeng.com/blog/2016/04/cors.html

CodePudding user response:

reference qybao reply: 3/f
this error is the problem of cross-domain request,
You understand the inner mechanism of CORS well,
https://www.ruanyifeng.com/blog/2016/04/cors.html

Sorry, I still don't quite understand I now how to solve

CodePudding user response:

Your front-end is deployed in the above?

If is use nginx, you can configure the proxy, such configuration
 
The location/front desk use background path/{
Proxy_set_header Host $Host;
Proxy_set_header X - Real - IP $remote_addr;
URL/proxy_pass corresponding background;
}


So the front is only a domain

This operation will be more formal, should not let users perceive the structure of the entire project

CodePudding user response:

refer to 4th floor from crossing the 96 reply:
sorry ah, I'm still not sure what should I do now how to solve the

You haven't good articles, article inside all tell you the solution, but you are not careful see, or don't understand,
Before you change a good cross domain problem is for a simple request, it is for a simple request, are you going to use your PUT the front end of the send a Custom Header information X - Custom - the Header,


CodePudding user response:

reference 5 floor BoRoBoRoMe reply:
your front-end is deployed in the above?

If is use nginx, you can configure the proxy, such configuration
 
The location/front desk use background path/{
Proxy_set_header Host $Host;
Proxy_set_header X - Real - IP $remote_addr;
URL/proxy_pass corresponding background;
}


So the front is only a domain

This operation will be more formal, should not let users perceive the structure of the entire project

So the configuration is there a problem? I deleted the cross-domain code in Java, all request the cross-domain, problem of cross-domain code does not delete in Java and title consistent description
 

Server {
Listen, 8888;
server_name localhost;
The location/{
Proxy_pass http://127.0.0.1:8081; # VUE program
}
The location/API {
Proxy_pass http://127.0.0.1:7777; # backend interface
}
}

CodePudding user response:

Tip made it very clear:
Because the requested resource is not set Access - Control - Allow - Origin
You add the Access Settings to return to the Header in the background - Control - Allow - Origin

CodePudding user response:

refer to the water's edge, 9/f, 2 reply:
prompt made it very clear:
Because the requested resource is not set Access - Control - Allow - Origin
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related