[英]jQuery AJAX call to Java RESTful web service is not working
我正在使用jQuery AJAX調用Java RESTful Web服務。 AJAX調用和RESTful都駐留在我的本地計算機中。 我無法得到答復。
這是我使用的代碼:
JSP:
$("#someCode").blur(function(){
$.support.cors=true;
var serviceAddress = '192.168.254.25:8080/WeightsAndMeasure/restful/sample/test';
$.ajax({
type: 'GET',
dataType: 'html',
url: serviceAddress,
crossDomain: true,
cache:false,
async:false,
success: function (data) {
alert("Data loaded: " + data);
},
error: function (xhr) {
alert(xhr.responseText+' 'xhr.status+' '+ xhr.statusText);
}
});
});
寧靜:
@Path("/sample")
public class SampleService
{
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/test")
public Response testService()
{
System.out.println("Inside testService");
return Response.ok("You got success.").header("Access-Control-Allow-Origin", "*").build();
}
}
當我通過瀏覽器URL嘗試時,它工作正常。 但是當我通過blur(function())
使用AJAX調用時,它不起作用。
如何調用restful並成功獲得響應?
您是否在項目中啟用了CORS(跨源資源共享過濾器)? 據我所知,如果要從外部“請求者”訪問Web服務,則必須啟用它
var serviceAddress = '192.168.254.25:8080/WeightsAndMeasure/restful/sample/test';
是錯的。 您必須設置“ localhost / ajax /”。
在腳本中嘗試此操作,並在“ / restful / sample / test”中控制其余代碼
var serviceAddress = '/restful/sample/test';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.