在jQuery AJAX调用中返回多个值 [英] Return multiple values in jQuery AJAX call
问题描述
我有一个jQuery post函数,单击div
后返回成功响应.但是,我想在成功时返回多个变量.我必须使用JSON吗?如果可以,是否可以在成功之后将其集成到$.ajax
函数中?
I have a jQuery post function that returns a response on success after the click of a div
. However, I would like to return multiple variables on success. Do I have to use JSON, and if so, is it possible to integrate it into the $.ajax
function after success?
$.ajax({
type: "POST",
data: "action=favorite&username=" + username + "&topic_id=" + topic_id + "&token=" + token,
url: "favorite.php",
success: function(response) {
}
});
编辑
我感谢每个人的帮助+1对所有人!
EDIT
I appreciate everyone's help + 1 to all!
推荐答案
最好仅使用来自服务器的JSON响应.这样,您的服务器后端将充当JSON-RPC服务器,而前端将完全独立于它!当然,您可以将JSON与$.ajax
函数一起使用.这是一个示例:
It would be a very good idea to use only JSON responses from the server. That way your server backend would act as a JSON-RPC server and the front-end would be completely independent of it! Of course you can use JSON with the $.ajax
function. Here's an example:
$.ajax({
url: 'http://some.url.com/',
data: 'some=post&data=xyz',
type: 'POST',
dataType: 'json',
success: function(response, statusText) {
// `response` here is a valid JSON object; jQuery handles the work of parsing the response, etc.
}
});
这篇关于在jQuery AJAX调用中返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!