2013-07-26 123 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字符串轉換爲對象,他有 –