JSP ajax跨域問題 怎麼處理 原因:CORS 頭缺少 'Access-Control-Allow-Origin')。 ajax http 415

來源:互聯網
上載者:User

標籤:vax   amp   資訊   pos   request   on()   tcl   跨域問題   row   

/**
* Project Name:cm2mManage
* File Name:CrossSiteFilter.java
* Package Name:com.yoxnet.serverframework.base
* Date:2016年4月27日下午4:52:51
* Copyright (c) 2016, [email protected] All Rights Reserved.
*
*/

package com.zhl.sms.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* ClassName:CrossSiteFilter <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2016年4月27日 下午4:52:51 <br/>
* @author YYC
* @version
* @since JDK 1.6
* @see
*/
public class CrossSiteFilter implements Filter{

private Logger logs = LoggerFactory.getLogger(this.getClass());

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
Enumeration e = req.getHeaderNames();
//String headValue = "";
//while(e.hasMoreElements()){
// headValue = (String) e.nextElement();
// String value = req.getHeader(headValue);
// System.out.println(headValue+"="+value);
// }
logs.debug(">>>>>>>>>>>>>>>本次請求的header資訊如下:");
while(e.hasMoreElements()){
String name = (String) e.nextElement();
String value = req.getHeader(name);
logs.debug(name+"="+value);
}
logs.debug(">>>>>>>>>>>>>>>本次請求的header資訊結束");
HttpServletResponse resp = (HttpServletResponse) response;
//HttpSession session = req.getSession();
resp.addHeader("Access-Control-Allow-Origin", "*");
//resp.addHeader("Access-Control-Allow-Origin", "http://192.168.1.170");
resp.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
resp.addHeader("Access-Control-Allow-Credentials","true");
resp.addHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type");
resp.addHeader("Access-Control-Max-Age", "1800");//30 min
if (req.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(req.getMethod())) {

}
chain.doFilter(request, response);
}

@Override
public void destroy() {

}

}

 

 

 

xml配置中:

<filter>
<filter-name>crossSiteFilter</filter-name>
<filter-class>com.zhl.sms.filter.CrossSiteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>crossSiteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

JSP ajax跨域問題 怎麼處理 原因:CORS 頭缺少 'Access-Control-Allow-Origin')。 ajax http 415

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.