从javascript调用java restful Web服务时返回值失败
我有一个用java编写的restful Web服务。我用javascript调用它 当我调用webservice时,所有代码都能完美地工作,但并不能完美地返回值。我的代码如下: ws:从javascript调用java restful Web服务时返回值失败,java,javascript,web-services,rest,Java,Javascript,Web Services,Rest,我有一个用java编写的restful Web服务。我用javascript调用它 当我调用webservice时,所有代码都能完美地工作,但并不能完美地返回值。我的代码如下: ws: @RestController public class VillageService { @Autowired private InnerService innerService; @RequestMapping(value = "/services/getVillages") public
@RestController
public class VillageService {
@Autowired
private InnerService innerService;
@RequestMapping(value = "/services/getVillages")
public Village getAllVillages(@RequestParam int param) throws JSONException {
long startTime = System.currentTimeMillis();
Village result = innerService.getAllVillagesCacheable(param);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("süre: " + elapsedTime);
startTime = System.currentTimeMillis();
Village result2 = innerService.getAllVillages(param);
stopTime = System.currentTimeMillis();
elapsedTime = stopTime - startTime;
System.out.println("süre cache'siz: " + elapsedTime);
return result;
}
}
function callWS()
{
$.ajax({
type: 'GET',
url: 'http://localhost/services/getVillages/',
data: "param=5", // the data in form-encoded format, ie as it would appear on a querystring
dataType: "json", // the data type we want back, so text. The data will come wrapped in xml
success: function (data) {
alert("party hard"); // show the string that was returned, this will be the data inside the xml wrapper
},
error: function (data) {
alert("restful cagirmada hata"); // show the string that was returned, this will be the data inside the xml wrapper
}
});
};
Js:
@RestController
public class VillageService {
@Autowired
private InnerService innerService;
@RequestMapping(value = "/services/getVillages")
public Village getAllVillages(@RequestParam int param) throws JSONException {
long startTime = System.currentTimeMillis();
Village result = innerService.getAllVillagesCacheable(param);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("süre: " + elapsedTime);
startTime = System.currentTimeMillis();
Village result2 = innerService.getAllVillages(param);
stopTime = System.currentTimeMillis();
elapsedTime = stopTime - startTime;
System.out.println("süre cache'siz: " + elapsedTime);
return result;
}
}
function callWS()
{
$.ajax({
type: 'GET',
url: 'http://localhost/services/getVillages/',
data: "param=5", // the data in form-encoded format, ie as it would appear on a querystring
dataType: "json", // the data type we want back, so text. The data will come wrapped in xml
success: function (data) {
alert("party hard"); // show the string that was returned, this will be the data inside the xml wrapper
},
error: function (data) {
alert("restful cagirmada hata"); // show the string that was returned, this will be the data inside the xml wrapper
}
});
};
当我调用ws时,所有的“prinln”代码都能正常工作,但不返回值。我的js没有以“成功”结束,它陷入了“错误:”的情况
如何修复它?您应该使用AJAX调用传递数据,如下所示:
data: {param:5},
使用HTTP请求读取参数,还需要对返回JSON的方法进行@ResponseBody
注释:
@RequestMapping(value = "/services/getVillages")
@ResponseBody
public Village getAllVillages(HttpServletRequest request) throws JSONException {
String param = request.getParameter("param");
对
结果执行System.out.println
并检查浏览器控制台是否有任何错误。对结果执行System.out.println
并检查浏览器控制台是否有任何错误。我收到。。必需的跨源资源共享(CORS)错误好,这是你可以在谷歌上搜索的东西:在这里尝试建议的解决方案:thx回复。我找到了解决办法