简体   繁体   English

jQuery AJAX对Java RESTful Web服务的调用不起作用

[英]jQuery AJAX call to Java RESTful web service is not working

I am using jQuery AJAX to call a Java RESTful web service. 我正在使用jQuery AJAX调用Java RESTful Web服务。 Both AJAX call and RESTful are residing in my local machine. AJAX调用和RESTful都驻留在我的本地计算机中。 I am not able to get the response. 我无法得到答复。

Here is the code which I used: 这是我使用的代码:

JSP: 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);
         }
  });
});

Restful: 宁静:

@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();     
  }
}

When I am trying it through browser URL, it is working fine. 当我通过浏览器URL尝试时,它工作正常。 But when I am using AJAX call by blur(function()) , it is not working. 但是当我通过blur(function())使用AJAX调用时,它不起作用。

How to call the restful and get the response successfully? 如何调用restful并成功获得响应?

Have you enable CORS (Cross-Origin Resource Sharing Filter) on your project? 您是否在项目中启用了CORS(跨源资源共享过滤器)? As I know, you must enable it if you want to access the web service from external "requester" 据我所知,如果要从外部“请求者”访问Web服务,则必须启用它

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

is wrong. 是错的。 You have to set "localhost/ajax/". 您必须设置“ localhost / ajax /”。

Try this in your script and control your restfull code is in "/restful/sample/test" 在脚本中尝试此操作,并在“ / restful / sample / test”中控制其余代码

var serviceAddress = '/restful/sample/test';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM