跨域解决方案
1.加請求頭
后臺response添加header,response.setHeader("Access-Control-Allow-Origin", "*");?
2.jsonp
前端主要代碼(B項目---》請求A項目)
| $.ajax({ ???????? type :?"GET", ???????? async :?false, ???????? url :?"http://localhost:8080/getInfoA?username=testB", ???????? dataType :?"jsonp",//數(shù)據(jù)類型為jsonp? ???????? jsonp :?"jsonpCallback",//服務(wù)端用于接收callback調(diào)用的function名的參數(shù)? ???????? success :?function(data) { ??????????? alert(data.result+data.username); ???????? }, ???????? error :?function() { ??????????? alert('fail'); ???????? } ????? }); | 
后臺主要代碼(A項目)
@RequestMapping("/getInfoA")@ResponseBodypublic String getInfo(HttpServletRequest request, HttpServletResponse response){String username = request.getParameter("username");String jsonpCallback = request.getParameter("jsonpCallback");JSONObject resultJSON = new JSONObject();try {resultJSON.put("result", "跨域請求成功");resultJSON.put("username", username);} catch (JSONException e) {e.printStackTrace();}return jsonpCallback + "(" + resultJSON.toString() + ")";}結(jié)果顯示
?
3.nginx或者springcloud 搭建網(wǎng)關(guān)
DEMO:https://pan.baidu.com/s/1w73vm9rhqLRFELyiuU9BSg
?
轉(zhuǎn)載于:https://www.cnblogs.com/liclBlog/p/9657098.html
總結(jié)
 
                            
                        - 上一篇: salesforce零基础学习(八十九)
- 下一篇: rhel7+apache+c cgi+动
