js跨域问题新方案
只要创建一个空图片。
js代码:
var data = "http://localhost:8080/test?id="+id+"&content="+encodeURIComponent(content); //序列化表单数据
new Image().src=data;
后端返回一个空图片(后端采用mina框架),核心代码如下:
在MessageHandler的实现类中,
Class MyMessageHandler implements MessageHandler{
public void handle(IoSession session, Message message) {
..........................
response.setStatus(200);
response.setStatusText("OK");
response.setHeaders(new String[]{
"Server: AsmeServer",
"Pragma: No-cache",
"Cache-Control: no-cache",
"Expires: Thu, 01 Jan 1970 00:00:00 GMT",
"P3P: CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'",
"Set-Cookie: opt=" + (inout ? "0" : "1") + COOKIE_ATTR,
"Content-Type: image/gif",
"Content-Length: 43",
"Connection: close"
});
response.setContent(CookieUtils.IMG11);
session.write(response).addListener(IoFutureListener.CLOSE);
...............................
}
}