[英]Get JSON from response object
<html>
<body>
<form action="login.do" method="post">
//.....
<input type="submit" value ="send">
</form>
</body>
</html>
In my servlet I will be handle the request and send the json response back. 在我的servlet中,我将处理请求并将JSON响应发送回去。 How can I get the json object from the response ?
如何从响应中获取json对象?
But we can do this by calling a function when we click the button. 但是我们可以通过单击按钮时调用一个函数来实现。
function(){
$ajax(
url:"login.do"
success: function(data){
//.....
}
)
}
is there any way to do this? 有什么办法吗? Or only using the function call we can do it?
还是仅使用函数调用就可以做到?
You could set the dataType
to json
, or you could use the short cut method $.getJSON() . 您可以将
dataType
设置为json
,也可以使用快捷方式$ .getJSON() 。
$.getJSON(your_url, function(data) {
// data here is already an object.
console.log(data);
});
EDIT: getJSON
will use GET
request type, for POST
, you could do 编辑:
getJSON
将使用GET
请求类型,对于POST
,您可以执行
$.post(your_url, function(data) {
// data here is already an object.
console.log(data);
}, 'json');
first argument of success callback that you call data
is the json object so long as you include dataType:'json'
in ajax options or use shorthand $.post(url[,data][,function(json){}),'json'])
method 只要您在ajax选项中包含
dataType:'json'
或使用简写$.post(url[,data][,function(json){}),'json'])
,调用data
的成功回调的第一个参数就是json对象。 $.post(url[,data][,function(json){}),'json'])
方法
Read about success callback in $.ajax API : 了解有关$ .ajax API中的成功回调的信息:
http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/
EDIT: Using deffered method 编辑:使用递延方法
var ajaxCall= $.post( url, dataToServer,'json')
$.when( ajaxCall).then(function(data){
var json =data;
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.