java跨域访问问题记录

跨域访问记录

package com.alphasta.zsbs.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.alphasta.zsbs.util.Resources;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yx.frame.jdbc.Sql;
import com.yx.frame.jdbc.SqlEntry;
import com.yx.frame.jdbc.Table;
/**
* 更新用户信息
* @author D
*
*/
public class UpdateUserInfoServlet extends HttpServlet {

private Logger log = Logger.getLogger(UpdateUserInfoServlet.class);
/**
*
*/
private static final long serialVersionUID = 3869311915733581508L;

/**
* Constructor of the object.
*/
public UpdateUserInfoServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("不支持GET请求");
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Credentials","true");//中间测试过程一直有问题,加了这个就好了
response.setHeader("Access-Control-Allow-Headers", "origin, x-csrftoken, content-type, accept, x-auth-code, X-App-Id, X-Token");
response.setHeader("Access-Control-Allow-Method", "POST, GET, OPTIONS");
response.setHeader("Access-Control-Allow-Origin", "http://XXXX");//注意加自己地址
response.setContentType("application/json; charset=utf-8");

String userJson = "";

//逻辑代码部分

log.info("update post --"+userJson);
OutputStream re_out = response.getOutputStream();
re_out.write(userJson.getBytes("UTF-8"));
re_out.flush();
log.info("update post finish --"+userJson+"--"+userJson.getBytes("UTF-8"));
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

时间: 2024-08-28 17:04:04

java跨域访问问题记录的相关文章

Ajax + java 跨域访问Json

使用jsonp跨域获取json数据.Ajax获取JAVA服务器json数据. 在Servlet 中代码: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Access-Control-Allow-Origin", "*");//开启CORS,

java 设置允许ajax XMLHttpRequest 请求跨域访问

在非IE下,使用XMLHttpRequest 不能跨域访问, 除非要访问的网页设置为允许跨域访问. 将网页设置为允许跨域访问的方法如下: Response.AddHeader("Access-Control-Allow-Origin", "*"); 或指定域名下可以访问: Response.AddHeader("Access-Control-Allow-Origin", "http://www.1688hot.com:80")

JAVA Serverlet 允许跨域访问的 Api

//允许跨域访问 response.addHeader("Access-Control-Allow-Origin", "*"); //设置输出的编码为 UTF-8 response.setCharacterEncoding("UTF-8"); //返回给浏览器 response.getWriter().write("跨域成功");

js,java,ajax实现跨域访问及其原理

http://blog.csdn.net/saytime/article/details/51540876 这篇文章对跨域访问做了较为细致得分析,我这里做下简单总结 1.实现跨域访问原理: 浏览器由于同源策略不能做跨域访问,但是<script>标签是开放策略能够跨域访问,所以突破口就是通过<script>来实现跨域访问获得其他地方得json数据 jsonp模式: JSONP(JSON with Padding)是JSON的一种"使用模式",可用于解决主流浏览器的

Ajax跨域访问wcf服务中所遇到的问题总结。

工具说明:vs2012,sql server 2008R2 1.首先,通过vs2012建立一个wcf服务项目,建立好之后.再新开一个vs2012 建立web项目,通过jQuery的ajax方法访问服务. 问题:由于web项目和wcf服务,不在同一个端口之中,所以涉及到“跨域”的问题.跨域访问的时候,需要对服务的接口和方法做一定的限定.具体参考:http://www.cnblogs.com/yangbingqi/p/2096197.html 2.解决了跨域问题,我们需要把服务部署到IIS.vs20

JQuery+ajax+jsonp 跨域访问

Jsonp(JSON with Padding)是资料格式 json 的一种“使用模式”,可以让网页从别的网域获取资料. 关于Jsonp更详细的资料请参考http://baike.baidu.com/view/2131174.htm,下面给出例子: 一.客户端 Html代码   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.

三种方法实现js跨域访问

javascript跨域访问是web开发者经常遇到的问题,什么是跨域,一个域上加载的脚本获取或操作另一个域上的文档属性,下面将列出三种实现javascript跨域方法: 1.基于iframe实现跨域 基于iframe实现的跨域要求两个域具有aa.xx.com,bb.xx.com这种特点,也就是两个页面必须属于一个基础域(例如都是xxx.com,或是xxx.com.cn),使用同一协议(例如都是 http)和同一端口(例如都是80),这样在两个页面中同时添加document.domain,就可以实

浅析JSONP-解决Ajax跨域访问问题

浅析JSONP-解决Ajax跨域访问问题 很久没有写随笔了,总是感觉没时间,其实时间就是...废话少说,前几天,工作上有一新需求,需要前端web页面异步调用后台的Webservice方法返回信息.实现方法有多种,本例采用jQuery+Ajax,完成后,在本地调试了一切ok,但是部署到服务器上以后就出现问题了,后台服务调用没有响应,怎么回事?代码没怎么改动,唯一修改的地方就是jQuery的ajax方法中的url地址.难道是这里的问题,经过检查和调试,发现原来是同源策略在作怪,我们知道,JavaSc

vue + vue-resource 跨域访问

使用vue + vue-resource进行数据提交,后台使用RESTful API的方式存取数据,搞了一天,终于把后台搞好了.进行联合调试时,数据不能提交,报403错误: XMLHttpRequest cannot load http://localhost:8090/xxx/xxx. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header