在 jQuery AJAX 调用中返回多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2571702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:49:22  来源:igfitidea点击:

Return multiple values in jQuery AJAX call

jqueryjson

提问by Scarface

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 $.ajaxfunction after success?

我有一个 jQuery post 函数,它在单击div. 但是,我想在成功时返回多个变量。我是否必须使用JSON,如果是这样,成功$.ajax后是否可以将其集成到函数中?

$.ajax({
   type: "POST",
   data: "action=favorite&username=" + username + "&topic_id=" + topic_id + "&token=" + token,
   url: "favorite.php",
   success: function(response) {

   }
});


EDIT
I appreciate everyone's help + 1 to all!

编辑
我感谢大家的帮助+ 1!

回答by themoondothshine

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 $.ajaxfunction. Here's an example:

仅使用来自服务器的 JSON 响应将是一个非常好的主意。这样,您的服务器后端将充当 JSON-RPC 服务器,而前端将完全独立于它!当然,您可以将 JSON 与该$.ajax函数一起使用。下面是一个例子:

$.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.
    }
});

回答by Quentin

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.

我有一个 jquery post 函数,它在单击 div 后返回成功的响应。但是,我想在成功时返回多个变量。

You can only return one value - a blob of text (in most cases).

您只能返回一个值 - 一团文本(在大多数情况下)。

You can, however, structure that text, so you can easily extract different bits of data from it.

但是,您可以构造该文本,因此您可以轻松地从中提取不同的数据位。

Do I have to use JSON

我是否必须使用 JSON

No, but it is the simplest option.

不,但这是最简单的选择。

, and if so, is it possible to integrate it into the $.ajax function after success?

,如果是这样,是否可以在成功后将其集成到$.ajax函数中?

Umm. Yes. Did you read the manual for the jQuery ajax function? It explicitly mentions using JSON and getting an object as the argument to the success function.

嗯。是的。您是否阅读了jQuery ajax 函数的手册?它明确提到使用 JSON 并获取一个对象作为成功函数的参数。

回答by Jim Schubert

You would have to return JSON (or some other data format supported by jQuery's ajax() function) from favorite.php.
edit: it doesn't haveto be json, but for multiple return values, it is the easiest to parse. For instance, if you returned xml or html, you'd have to traverse nodes to return a value.

您必须从favorite.php 返回JSON(或jQuery 的ajax() 函数支持的其他一些数据格式)。
编辑:它不具备成为JSON,但多个返回值,这是最简单解析。例如,如果您返回 xml 或 html,则必须遍历节点以返回值。

For instance, if you returned:
{"user": "Joe", "success" : "pass", "message" : "favorite added" }

例如,如果您返回:
{"user": "Joe", "success" : "pass", "message" : "favorite added" }

in success, you would use:

如果成功,您将使用:

function(response){
  var user = response.user;
  var success = response.success; // etc.
}

The important thing to remember is to specify the dataType in your ajax call as json. jQuery also supports the other dataTypes: xml, html, script, jsonp, text

要记住的重要一点是将 ajax 调用中的 dataType 指定为 json。jQuery 还支持其他数据类型:xml、html、script、jsonp、text

I believe the default is html. And, having written php to return xml with properly formatted headers in the php script, I can tell you that sometimes you haveto specify the dataType for jQuery to parse it correctly.

我相信默认是 html。而且,在编写 php 以在 php 脚本中返回带有正确格式标题的 xml 之后,我可以告诉您,有时您必须为 jQuery 指定 dataType 才能正确解析它。

回答by Ricardo Marimon

Let's say the returned json is something like this

假设返回的 json 是这样的

{
  firstName: 'Ricardo',
  lastName: 'Jones',
  errors: 0
}

You can use the jQuery getJSON method as follows:

您可以使用 jQuery getJSON 方法,如下所示:

$.getJSON(
   'favorite.php',
   { 'action': 'favorite', 'username': username, 'topic_id': topic_id, 'token': token },
   function(data) {
     alert(data.firstName);
     alert(data.lastName);
     alert(errors);
   }
)

In the returning function you can get the many variables you want.

在返回函数中,您可以获得所需的许多变量。

回答by Pawan Mishra

I have never programmed in PHP. In asp.net JSON is the default mode of data exchange in async webservice call , so as a developer I dont have to worry about underlying details of JSON. I guess even jQuery gets its data in JSON format. If in case you have multiple values , you can get them in the form of list or dictionary format.

我从来没有用 PHP 编程过。在 asp.net 中,JSON 是 async webservice call 中数据交换的默认模式,因此作为开发人员,我不必担心 JSON 的底层细节。我猜即使 jQuery 也以 JSON 格式获取其数据。如果您有多个值,您可以以列表或字典格式获取它们。