已拦截跨源请求:同源策略禁止读取位于 http://localhost:8080/*的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。

package com.ajax.TestServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = -925897313545216227L;
    @Override
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        /**
         * front request ajax.html
         *     
            
            
            
            Insert title here
            
            
            
            
            
            
         *
         */
        /**
         * 已拦截跨源请求:同源策略禁止读取位于 http://localhost:8080/wo.ajax 的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')
         * case when condition 1 (cause) : file:///C:/Users/Administrator/Desktop/ajax.html
         * otherwise condition 2 : http://localhost:8080/
         */
        //response.setHeader("Access-Control-Allow-Origin","*");
        StringBuffer sb = new StringBuffer();
        sb.append("{\"name\":\"");
        sb.append("Ajax");
        sb.append("\",\"code\":\"");
        sb.append("200");
        sb.append("\"");
        sb.append("}");
        System.out.println("running ");
        PrintWriter printWriter = response.getWriter();
        printWriter.println(sb.toString());
        printWriter.flush();
        printWriter.close();
        /**
         *
         *
                 xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
            
                MyServlet
                com.ajax.TestServlet.MyServlet
                1
            

            
                MyServlet
                *.ajax
            

            
                ajax.html
            

        

         */
    }
}


你可能感兴趣的:(已拦截跨源请求:同源策略禁止读取位于 http://localhost:8080/*的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。)