jsonp 跨域

1、js

$.ajax({
            type: "get",
            url: url,
            async: false,
            dataType: "jsonp",
            jsonp: "callback",
            success: function(data) {

          if('ok'==data.result){
              window.location.href='#pagetwo';
              updateDate();
          }else{

          }

            }
        });

2、 返回jsonp的util

protected  void writeCallbackUTF(String str, HttpServletRequest req,HttpServletResponse res){
		res.setCharacterEncoding("utf-8");
		String callback=req.getParameter("callback");
		PrintWriter out;
		try {
			out = res.getWriter();
			out.write(callback+"("+str+")");
			out.flush();
			out.close();
			return;
		} catch (IOException e) {
			logger.error("回执失败!", e);
		}
		logger.info(this.getClass().getName() + ".writeString():end");
	}

  3、调用  writeCallbackUTF() 方法

  

writeCallbackUTF("{\"result\":false}", req, res);

  

posted @ 2016-10-10 11:25  咸蛋超人、  阅读(130)  评论(0编辑  收藏  举报