我的内容标题显示响应为字符串,即使我已从我的其余服务返回Json数据。这个问题特别发生在ajax函数调用的post请求中?
<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);
}
});
}
使用帖子注释插入数据的我的休息服务代码:
@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 :(得分:0)
替换:
success:function(feeds)
{
alert(feeds);
}
到
success:function(feeds)
{
var obj = jQuery.parseJSON(feeds);
console.log(obj);
}
注意:现在您可以使用var obj,因为当您从Rest服务获得响应时,它是一个json字符串,因此您必须将其转换为object。