首页 > 代码库 > 两个java项目,跨域访问时,浏览器不能正确解析数据问题
两个java项目,跨域访问时,浏览器不能正确解析数据问题
@Controller
@RequestMapping(value = "http://www.mamicode.com/api")
public class ApiItemCatController {
@Autowired
private ItemCatService itemCatService;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> queryItemCatAll(@RequestParam(value="http://www.mamicode.com/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项目,跨域访问时,浏览器不能正确解析数据问题