有没有什么好点的工具使用js来访问rest webservice ?
要求能使用post方式访问, 并且能携带json类型的请求体  。 还要能处理返回的json内容
下面是我用jquery的ajax方法来请求的, 但是有一个问题。 服务端已经正确返回了,浏览器的response的responseStatus的值也是200 ,但是alert(success)不会执行,alert("error")和alert("complete");都会执行var getRecentlyURL = "http://localhost:8888/recently_post" ; 
function requestRecentlyPOST(userId , lastPostId , content){
var content = genRequestBody(userId , lastPostId , content) ; //得到json字符串参数
$.ajax({                 
    type: "POST", 
    url: getRecentlyURL ,   
contentType :"text/plain; charset=UTF-8",    
     data:content      
                       
}).done(function() {
    alert( "success" );
  })
  .fail(function(r) {
    alert( "error" + r);
  })        
  .always(function() {
    alert( "complete" );             
  });    
}

解决方案 »

  1.   

       type: "post",
                        ///
                        success: function(msg) {
                            alert(msg);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest.status);
                            alert(XMLHttpRequest.readyState);
                            alert(textStatus);
                        },
    看看错误信息。
      

  2.   


    var content = genRequestBody(userId , lastPostId , content) ;
     $.ajax({
             type: "post",  
             contentType :"text/plain; charset=UTF-8",
             url: getRecentlyURL, 
             data: content,    
             dataType: 'json',
             success: function(result) {
                 alert(result);        
             },     
     error: function(XMLHttpRequest, textStatus, errorThrown) {
             alert(XMLHttpRequest.status);
             alert(XMLHttpRequest.readyState);
             alert(textStatus);
         }
         });  进了error
    alert的3个值依次是 0 、 0、 error
      

  3.   

     // contentType :"text/plain; charset=UTF-8",
    这个注释掉。试试看。
      

  4.   

    成功返回了, 我在后端看了的,是成功返回的。  使用firebug也看到response的status=200 , 但是不清楚js里面为什么会这样
      

  5.   

    还是不行 。这是后端的log ,status=2002013-10-31 17:08:48 org.restlet.engine.log.LogFilter afterHandle
    信息: 2013-10-31 17:08:48 127.0.0.1 - - 8888 POST /recently_post - 200 - 59 677 http://localhost:8888 Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0 -
      

  6.   

    你用 ie 试试看看。你服务器端返回的是json是吧。
      

  7.   

    ie也不行 , 我直接用firefox的rest图形界面插件访问都能正确返回。 
    但是就是js不行
      

  8.   

    看不出来。如果参数有问题。 你后台就不会进去。很奇怪。你直接浏览器访问呢。http://localhost:8888/recently_post