1. 程式人生 > >兩個java項目,跨域訪問時,瀏覽器不能正確解析數據問題

兩個java項目,跨域訪問時,瀏覽器不能正確解析數據問題

back false callback require autowire data category 解析 als

@Controller
@RequestMapping(value = "api")
public class ApiItemCatController {

@Autowired
private ItemCatService itemCatService;

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> queryItemCatAll(@RequestParam(value="callback",required =false)String callback) {
try {
// 調用service 查詢
ItemCatResult result = itemCatService.queryItemCatAll();
ObjectMapper mapper = new ObjectMapper();
String jsonResult = mapper.writeValueAsString(result);
//判斷是否是跨域請求
if(StringUtils.isNotEmpty(callback)){
return ResponseEntity.ok(callback + "("+jsonResult+")");
}
// 如果不是,直接返回結果和成功狀態碼
// 返回結果和狀態碼
return ResponseEntity.status(HttpStatus.OK).body(jsonResult);
} catch (Exception e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
null);

}
}

=================================================================================================================

另一個系統的url :http://manage.taotao.com/rest/api/item/cat?callback=category.getDataService

兩個java項目,跨域訪問時,瀏覽器不能正確解析數據問題