开发者

How to return multiple values from JQuery AJAX call?

开发者 https://www.devze.com 2023-02-07 03:01 出处:网络
I\'m making an AJAX call to a page that returns XM开发者_如何学PythonL.It turns out that I need to also return another standalone value, along with the XML.

I'm making an AJAX call to a page that returns XM开发者_如何学PythonL. It turns out that I need to also return another standalone value, along with the XML.

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];
}

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...

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.


Wow. Should've been:

var **parsed**   = data.split('DELIMITER');
var xml      = **parsed**[0];
var myvalue  = **parsed**[1];


Just to add to your answer.

the reason you are seeing < and ? is because data is text which is an array of characters and as the xml starts as <?xml the first and second (0th and 1st) values are < and ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号