如何从JQuery的AJAX调用返回多个值? [英] How to return multiple values from JQuery AJAX call?
问题描述
我在做一个AJAX调用的页面返回的XML。事实证明,我还需要返回另一个独立的价值,随着XML。
I'm making an AJAX call to a page that returns XML. It turns out that I need to also return another standalone value, along with the XML.
下面是JQuery的AJAX调用:
Here is the JQuery AJAX call:
$.ajax({
type: "GET",
url: "filesearch.asp",
data: "action=getresponse,
dataType: "text",
cache: false,
success: function(data){
var parsed = data.split('DELIMITER');
var xml = data[0];
var myvalue = data[1];
}
发送响应的页面发送由 DELIMITER 字符串分隔的XML和我的价值。另外,我设置具体的数据类型,以便它把完整的响应,一个文本,我假设我应该能够简单地分割字符串的分隔符和访问我的成功的功能这两个值。萤火虫显示我正确地得到充分的反应,但是当我登录 XML 和 myvalue的到控制台,我得到:
The page that sends the response sends the XML and my value separated by the DELIMITER string. Also, I set the dataType so that it treats the full response as a text, and I'm assuming I should be able to simply split the string at the delimiter and access both values in my success function. Firebug shows I get the full response correctly, but when I log xml and myvalue to the console, I get:
xml = <
myvalue = ?
任何想法,我做错了,或如何解决?
Any ideas what I'm doing wrong or how to troubleshoot?
推荐答案
正如你已经注意到了:你必须使用解析
而不是数据
...
As you already noticed: you have to use parsed
instead of data
...
但不要使用纯'text,而是JSON作为您的数据类型和改变你的ASP脚本输出有效的JSON。
然后数据
是一个JavaScript对象,这样你就不必浪费时间与分割字符串等。
But: Do not use plain-'text' but 'json' as your data type and change your asp script to output valid JSON.
Then data
is a JavaScript object so you don't have to mess around with splitting strings etc.
这篇关于如何从JQuery的AJAX调用返回多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!