AngularJS+Servlet跨域问题
来源:互联网 发布:python beginwith 编辑:程序博客网 时间:2024/06/05 19:24
一、JSONP
Controller代码如下:
var myUrl = "http://localhost:8080/domainServer/TestServlet?callback=JSON_CALLBACK";$http({method:"JSONP", url: myUrl, cache: false}).success( //cache为true时只向服务器发送一次请求 function (data, status, headers, config) { console.log(data); alert(data.name); }).error( function (data, status, headers, config) { console.log(data); alert(data); });
或者
var myUrl = "http://localhost:8080/domainServer/TestServlet?callback=JSON_CALLBACK";$http.jsonp(myUrl,{cache: false}).success( function(data, status, headers, config){ console.log(data); alert(data.name); }).error( function (data, status, headers, config) { console.log(data); alert(data); });
private void jsonp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json;charset=UTF-8"); resp.addHeader("Access-Control-Allow-Origin", "*"); //文件头中增加该属性,支持跨域 PrintWriter out = resp.getWriter(); String callback = req.getParameter("callback"); //获取参数 StringBuffer sb = new StringBuffer(callback); sb.append("("); sb.append("{age:" + 18 + ", name: 'Tom'}"); //sb.append("'test string ...'"); sb.append(")"); out.print(sb.toString()); //拼接字符串JSON_CALLBACK(Object),Object为json串或加引号的字符串 out.flush(); out.close();}console.log()输出:Object {age: 18, name: "Tom"}
二、Post请求
Controller代码如下:
var myUrl = "http://localhost:8080/domainServer/PostServlet";$http.post(myUrl, {withCredentials:true, testName:"Tom"}).success( function (data, status) { console.log(data); console.log(status); });Servlet代码如下:
private void post(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("application/x-www-form-urlencoded;charset=UTF-8"); //此处为application/x-www-form-urlencoded,否则无法解析resp.addHeader("Access-Control-Allow-Origin", "*"); //支持跨域PrintWriter out = resp.getWriter();String testName = req.getParameter("testName"); //获取表单元素StringBuffer sb = new StringBuffer();sb.append("test{dds(dge");out.print(sb.toString()); //任意字符串out.flush();out.close();}输出值:test{dds(dge
三、通过Script的src属性实现跨域(因为script不存在跨域的问题)
HTML代码:
<?DOCTYPE html><html><head><title>Ajax without XMLHttprequest</title></head><body><script name="code" class="javascript">function cb(result){ //注意名字必须相同alert(result.name); };function start() { var _script=document.createElement("script"); _script.src="http://localhost:8080/domainServer/test?callback=cb";//注意名字必须相同_script.id="new_src"; document.body.appendChild(_script); };start();</script></body></html>Servlet代码:
private void script(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("application/json;charset=UTF-8");PrintWriter out = resp.getWriter();String callback = req.getParameter("callback");StringBuffer sb = new StringBuffer();sb.append(callback);sb.append("(");sb.append("{age:" + 18 + ", name: 'Tom'}");sb.append(")");out.print(sb.toString());out.flush();out.close();}输出值:Tom
0 0
- AngularJS+Servlet跨域问题
- AngularJS跨域问题
- AngularJs 跨域问题
- angularjs 跨域请求问题
- angularjs 跨域访问问题
- angularjs解决跨域问题
- angularJs 解决跨域访问问题
- AngularJs服-jsonp解决跨域问题
- angularjs嵌套作用域问题
- AngularJS中$http.jsonp跨域请求问题
- AngularJs最简单解决跨域问题案例
- AngularJs中的JSONP跨域访问数据传输问题
- AngularJs 服务之jsonp解决跨域问题
- AngularJs最简单解决跨域问题jsonp案例
- angularjs-$http跨域访问报错的问题
- AngularJS中$http.jsonp跨域请求问题
- java servlet action 跨域问题
- Angularjs 跨域请求
- Eclipse 调整背景色
- 6.25 被虐记 3 期末测试 const
- 关于easyui datebox的记录
- 早绑定和迟绑定技术的通俗分析
- java泛型
- AngularJS+Servlet跨域问题
- 比较齐全的gdb调试c/c++介绍
- WPF中的TreeView如何定义模版,并绑定数据
- EasyUI回车提交表单
- Substring with Concatenation of All Words 字符串操作 匹配的字串数
- jQuery插件开发模式
- 说说实际项目中如何防止重复提交
- 导出Excel时出现错误 java.lang.IllegalStateException: getOutputStream() has already been called
- hdu3466 Proud Merchants