1. 程式人生 > >Jsonp跨域調用

Jsonp跨域調用

req entity todo 查詢 gen response 返回 pathvaria 結果


@Autowired
private UserService userService; // 請求方法 GET // URL http://sso.taotao.com/user/check/{param}/{type} /** * 檢查數據是否可用 * * @param param * @param type * @return */ @RequestMapping(value = "check/{param}/{type}", method = RequestMethod.GET) // @ResponseBody
public ResponseEntity<String> check(HttpServletRequest request, @PathVariable String param, @PathVariable Integer type) { try { Boolean bool = this.userService.check(param, type); // 1.獲取callback參數 String callback = request.getParameter("callback");
// 2.判斷callback是否為非空 String result = ""; if (StringUtils.isNotBlank(callback)) { // 如果為非空,則進行偽裝 // 3.對返回結果進行包裹,偽裝成js數據fun(true) result = callback + "(" + bool + ")"; } else { // 如果為空,不偽裝,直接返回 result = "" + bool; }
// 這就是一個查詢,返回200 return ResponseEntity.ok(result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // 如果服務器錯誤,返回500 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); }

把泛型由Boolean改為String。

Jsonp跨域調用