产生跨域问题的原因
跨域问题是浏览器同源策略限制,当前域名的js只能读取同域下的窗口属性。
哪些情况会产生跨域问题
一个网站的网址组成包括协议名,子域名,主域名,端口号。比如 ,其中https是协议名,www是子域名,github是主域名,端口号是80,当在在页面中从一个url请求数据时,如果这个url的协议名、子域名、主域名、端口号任意一个有一个不同,就会产生跨域问题。
解决跨域问题
解决跨域问题有以下方式
- 使用jsonp
- 服务端代理
==========================================================
以上是百度一下都可以查到的说明,我之前也查了很多文章提到jsonp或者后台代码代理请求解决跨域请求json数据的方案。
其实从项目A请求项目B的数据做显示 我选择了后台处理,原因是我的数据要处理一下重新封装再给前端
思路是 1.ajax请求自己的后台----> 2.java代码用HttpClients去请求B项目----> 3.拿到B项目的数据----> 4.封装数据json串返回前端
上代码看吧!!!!
$ajax({ url: 'serchPage.do', data: {}, dataType: "json", success: function(data){ data = data.data.rows[0]; }})
'serchPage.do'请求后台
@ResponseBody @RequestMapping("/serchPage") public String serchPage(HttpServletRequest request,Model model){ String keyword = request.getParameter("keyword"); if(WexinCommon.inquire(keyword)){ String json = HttpClientsUtil.get(WexinCommon.SEARCH_URL + "keyword=" + keyword);return json; }return null; }
HttpClientsUtil这个类提成common
package com.cn.common;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.StatusLine;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.apache.log4j.Logger;import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; /** * 跨域请求 * @author Administrator * */public class HttpClientsUtil { private static final CloseableHttpClient httpclient = HttpClients.createDefault(); private static final Logger log = Logger.getLogger(HttpClientsUtil.class); public static String get(String url) { try { HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response = null; response = httpclient.execute(httpGet); StatusLine statusLine = response.getStatusLine(); String result = ""; HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); return result; } catch (IOException e) { e.printStackTrace(); } return null; } public static String post(String url, Mapmap) throws ParseException, IOException{ String body = ""; //创建post方式请求对象 HttpPost httpPost = new HttpPost(url); //装填参数 List nvps = new ArrayList (); if(map!=null){ for (Entry entry : map.entrySet()) { if(null!=entry.getValue()){ nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } } //设置参数到请求对象中 httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); //执行请求操作,并拿到结果(同步阻塞) CloseableHttpResponse response = httpclient.execute(httpPost); //获取结果实体 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity, "UTF-8"); } EntityUtils.consume(entity); //释放链接 response.close(); return body; } }
同理post请求的数据封装成map对象
-------------------------------------------------------------------