jQuery在ajax调用中返回多个值 [英] Jquery return multiple values in ajax call

查看:110
本文介绍了jQuery在ajax调用中返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆