在我的代码中,我将 json 格式的用户名和密码提交给 restful 服务获取方法。作为回报,此方法返回 json 对象,我无法在 jquery 的两个文本框中显示该对象。
这是我的jsp页面:
<script>
$(document).ready(function () {
$("#btnSubmit").click(function () {
var uname = $("#uname").val();
var pwd = $("#pwd").val();
authenticate(uname, pwd);
});
});
//authenticate function to make ajax call
function authenticate(uname, pwd) {
$.ajax
({
type: 'GET',
//the url where you want to sent the userName and password to
url: 'REST/WebService/GetLogin',
dataType: 'json',
async: false,
//json object to sent to the authentication url
data:{'uname': uname, 'pwd': pwd },
error:function()
{
alert("unsuccessfull");
},
success:function(server_response)
{
$( '#message' ).text( server_response.message);
$( '#lastname' ).text( server_response.lastname);
alert("successfull");
}
});
}
</script>
<input type="text" id="message"></input><br/>
<input type="text" id="lastname"></input><br/>
UserName:<input type="text" name="uname" id="uname"/><br/>
Password:<input type="text" name="pwd" id="pwd"/><br/>
<input type="submit" name="btnSubmit" id="btnSubmit" value="LogIn"/>
我宁静的get方法:
@Path("/WebService")
public class LoginService{
@GET
@Path("/GetLogin")
@Consumes("application/json")
@Produces("application/json")
public String loginFeed(@QueryParam("uname") String uname,@QueryParam("pwd") String pwd )
{
String feeds = null;
try
{
ArrayList<FeedObjects> feedData = null;
ProjectManager projectManager= new ProjectManager();
feedData=projectManager.GetLogin(uname, pwd);
feeds=FeedTransformer.UserFeed(feedData);
System.out.println("inside");
}
catch (Exception e)
{
System.out.println("error");
}
return feeds;
}
此提要采用 json 对象的形式。