jquery
2013-05-08 144 views 0 likes 
0

我正在使用jQuery AJAX調用Java REST風格的Web服務。 AJAX調用和RESTful都駐留在本地機器中。我無法得到答覆。jQuery AJAX調用Java REST風格的Web服務不起作用

這裏是我使用的代碼:

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並獲得響應成功?

+0

清理你的代碼,請 – 2013-05-08 06:36:56

+0

提高你已經張貼了這個問題的方式。 – manish 2013-05-08 06:38:58

+0

可能是因爲違反同源策略,請檢查您的瀏覽器控制檯 – 2013-05-08 06:58:55

回答

0

您是否在項目上啓用CORS(跨源資源共享過濾器)? 據我所知,你必須,如果你想從外部「請求者」訪問Web服務

0
var serviceAddress = '192.168.254.25:8080/WeightsAndMeasure/restful/sample/test'; 

是錯誤的啓用它。你必須設置「localhost/ajax /」。

在你的腳本試試這個,控制你的REST類型的代碼是「/寧靜/樣品/測試」

var serviceAddress = '/restful/sample/test'; 
相關問題