2013-07-26 50 views
0

即使我从我的休息服务中返回了Json数据,我的内容标题也显示响应为字符串。这个问题特别发生在ajax函数调用中的post请求中?响应json数据

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">/script> 
function authenticate(Source,Destination) { 
    $.ajax 
    ({ 
     type: 'POST', 
     url: 'REST/WebService/InsertFeeds', 
     dataType:'json', 
     //async: false, 
     data:{'Source': Source, 'Destination': Destination }, 
     //headers: { 
     contentType: 'application/json', 
     //}, 
     error:function() 
     { 
      alert("unsuccessfull"); 
     }, 
     success:function(feeds) 
     { 
      alert(feeds); 
     } 
    }); 
    } 

我的REST服务代码数据使用后插入注释:

@Path("/WebService") 

public class LoginService 
{ 

    @POST 
    @Path("/InsertFeeds") 
    @Consumes("application/json") 
    @Produces("application/json") 
     public String messageFeed2(@FormParam("Source") String Source,@FormParam("Destination") String Destination) 
    { 
     String feeds = null; 
     try 
     { 

      String feedData=null; 
      ProjectManager projectManager= new ProjectManager(); 
      feedData=projectManager.InsertFeeds(Source,Destination); 
      feeds=FeedTransformer.UserFeeding(feedData);//converts string in Json format using gson library 

     } catch (Exception e) 
     { 
      System.out.println("error"); 
     } 
     return feeds; 
    } 


} 
+0

您面临的问题是什么? – Veera

+0

Json是一个字符串,所以当然它被检索,它不是一个对象,直到你将它转换为一个 –

+0

Firebug显示数据返回的响应部分不在json内容意味着json对象没有得到返回为什么? – shanky

回答

0

替换:

success:function(feeds) 
    { 
     alert(feeds); 
    } 

success:function(feeds) 
    { 
     var obj = jQuery.parseJSON(feeds); 
     console.log(obj); 
    } 

注:现在ÿ ou可以使用var obj,因为当你从Rest服务获得响应时,它是一个json字符串,所以你必须将它转换为对象。

+2

当数据类型设置为json时,jquery自动将有效的json字符串转换为对象,他有 –