作为JSONP从javascript调用Java Rest API将返回无效的标签错误
我创建了一个java Web服务来返回国家/地区列表作为JSONP从javascript调用Java Rest API将返回无效的标签错误,java,api,rest,jsonp,Java,Api,Rest,Jsonp,我创建了一个java Web服务来返回国家/地区列表 @RequestMapping(value = "/getcountrylist", method = RequestMethod.GET, headers = "Accept=application/json") public @ResponseBody @ApiIgnore Object getcountrylist(@RequestParam String pvtToken, @RequestParam String l
@RequestMapping(value = "/getcountrylist", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody
@ApiIgnore
Object getcountrylist(@RequestParam String pvtToken,
@RequestParam String lan) {
System.out.println(API_TAG + "Request recevied to get CountryList");
System.out.println("DB:"+dbName);
if (!this.pvtToken.equals(pvtToken)) {
CountryList countryList = new CountryList();
return new ResponseEntity<CountryList>(countryList,
HttpStatus.UNAUTHORIZED);
}
CountryList countryList = avlMobileAPIService.getCountryList(lan);
return new ResponseEntity<CountryList>(countryList, HttpStatus.OK);
}
上面的函数调用webservice并返回列表,但在客户端显示“无效标签错误”
{"countrylist":[{"countryId":"4","countryCodeAlpha2":"AF","countryCodeAlpha3":"AFG","countryName":"Afghanistan ","isdCode":"93"},{"countryId":"5","countryCodeAlpha2":"AL","countryCodeAlpha3":"ALB","countryName":"Albania ","isdCode":"355"},{"countryId":"6","countryCodeAlpha2":"DZ","countryCodeAlpha3":"DZA","countryName":"Algeria ","isdCode":"213"},{"countryId":"7","countryCodeAlpha2":"AS","countryCodeAlpha3":"ASM","countryName":"American Samoa ","isdCode":"684"}]}
我在某篇文章中发现,ajax调用需要JSONP,但返回JSON数据
解决方案是什么?请参阅此链接
或者用这种简单的方法试试
@RequestMapping(value = "/mobile_getcountrylist", method = RequestMethod.GET, produces = {"application/x-javascript"})
@ResponseBody
public Object mobile_getcountrylist( @RequestParam("callback") String jsonpCallback) {
System.out.println(API_TAG + "Request recevied to get CountryList");
CountryList countryList = avlMobileAPIService.getCountryList("en");
//countryList.setJsonCallback(jsonpCallback);
return convertToJsonP(countryList,jsonpCallback);
}
private String convertToJsonP(Object o,String jsonpCallback){
String outputmessage=null;
ObjectMapper mapper = new ObjectMapper();
try {
outputmessage=mapper.writeValueAsString(o);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(outputmessage!=null){
outputmessage=jsonpCallback + "(" + outputmessage + ")";
}
return outputmessage;
}
Javascript代码
$.ajax({
type: 'GET',
url: 'http://localhost:8080/api/mobile_getcountrylist',
crossDomain: true,
async: false,
jsonpCallback: 'jsonpCallback',
dataType: 'jsonp',
contentType:'application/json',
success: function(data) {
alert('ok');
}
});
请参阅此链接
或者用这种简单的方法试试
@RequestMapping(value = "/mobile_getcountrylist", method = RequestMethod.GET, produces = {"application/x-javascript"})
@ResponseBody
public Object mobile_getcountrylist( @RequestParam("callback") String jsonpCallback) {
System.out.println(API_TAG + "Request recevied to get CountryList");
CountryList countryList = avlMobileAPIService.getCountryList("en");
//countryList.setJsonCallback(jsonpCallback);
return convertToJsonP(countryList,jsonpCallback);
}
private String convertToJsonP(Object o,String jsonpCallback){
String outputmessage=null;
ObjectMapper mapper = new ObjectMapper();
try {
outputmessage=mapper.writeValueAsString(o);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(outputmessage!=null){
outputmessage=jsonpCallback + "(" + outputmessage + ")";
}
return outputmessage;
}
Javascript代码
$.ajax({
type: 'GET',
url: 'http://localhost:8080/api/mobile_getcountrylist',
crossDomain: true,
async: false,
jsonpCallback: 'jsonpCallback',
dataType: 'jsonp',
contentType:'application/json',
success: function(data) {
alert('ok');
}
});
我还在ajax调用中尝试了以下属性-Accept:“application/json”,contentType:“application/json”,但结果相同?(例如:返回JSON响应,其中包含在客户端上执行的后续javascript代码)。如果没有,那么您可能只想在
.ajax
方法中使用success
回调。从您的示例来看,它看起来像是普通的JSON
,而不是JSONP
。我不知道,我应该在java端做些什么来支持JSONP。我举了很多例子,你不需要。只需将现有的JSON
响应与.ajax
中的success
回调一起使用即可。将dataType
更改为json
,将为您解析结果,并将对象作为success
回调的第一个参数返回。我尝试了以下代码$.ajax({type:'GET',dataType:'json',url:,data:{pvtToken:'JXku56AE0067YtRUSAZEE',lan:'en}),contentType:“application/json”,成功:函数(数据,状态){alert('suces');},};返回错误“NetworkError:500 Internal Server error-”我还尝试在ajax调用中使用以下属性-Accept:“application/json”,contentType:“application/json”,但结果相同。REST API实际上支持JSONP吗?(例如:返回JSON响应,其中包含在客户端上执行的后续javascript代码)。如果没有,那么您可能只想在.ajax
方法中使用success
回调。从您的示例来看,它看起来像是普通的JSON
,而不是JSONP
。我不知道,我应该在java端做些什么来支持JSONP。我举了很多例子,你不需要。只需将现有的JSON
响应与.ajax
中的success
回调一起使用即可。将dataType
更改为json
,将为您解析结果,并将对象作为success
回调的第一个参数返回。我尝试了以下代码$.ajax({type:'GET',dataType:'json',url:,data:{pvtToken:'JXku56AE0067YtRUSAZEE',lan:'en}),contentType:“application/json”,成功:函数(数据,状态){alert('suces');},};返回错误“NetworkError:500内部服务器错误-”嗨,我知道这篇文章很旧,但它帮了我很多+1嗨,我知道这篇文章很老,但它帮了我很多+1.