两个java项目,跨域访问时,浏览器不能正确解析数据问题

时间:2021-04-29 23:21:55

@Controller
@RequestMapping(value = "api/item/cat")
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