0

by using "AJAX" we can load our files like xml/json. But is there any possibility to load at a time two xml's using AJAX or get.

AJAX:

$.ajax({
        url: 'test.xml',
        type: "get",
        context: this,
        success: function (data) {
            alert("success");
        },
        error: function () {
            alert("failure");
        }
    });

Thanks in Advance.

Lucky
  • 425
  • 3
  • 8
  • 19

2 Answers2

2

Intro

It can be done easily, jQuery even provides method that will help you done this in a heartbeat. Basically all you need is to chain several ajax calls and process then with $.when when they are done.

Example

Working example: http://jsfiddle.net/Gajotres/qNJS8/

In this example you can see 3 distinct calls using $.ajax function. Also a warning, because jsFiddle don't support ajax calls fetching xml all data retrieved are dummy data, you will only need to replace it wit a proper URL.

Javascript:

$(document).on('pagebeforeshow', '[data-role="page"]',function(e,data){  
    // Multiple Ajax Requests 
    $.when( 
        parseXML({xml: "<cars><car><name>TOYOTA</name><country>JAPAN</country><pic>http://1call.ms/Websites/schwartz2012/images/toyota-rav4.jpg</pic><description>Toyota has announced that it will recall a total of 778,000 units that may have been manufactured with improperly-tightened nuts on the rear suspension.</description></car></cars>"}) , 
        parseXML({xml: "<cars><car><name>RENAULT</name><country>FRANCE</country><pic>http://cdn2.carsdata.net/thumb/182x105/pics/Renault/renault-sandero-16-stepway-01.jpg</pic><description>Renault Sandero 16 Stepway - this car is manufactured by renault. Renault Sandero 16 Stepway - Get car information and expert advice from CarsData.</description></car></cars>"}) , 
        parseXML({xml: "<cars><car><name>AUDI</name><country>GERMANY</country><pic>http://img2.netcarshow.com/Audi-RS6_Avant_2014_800x600_wallpaper_06.jpg</pic><description>The new Audi RS6 Avant unites 412 kW (560 hp) of power and 700 Nm (516.29 lb-ft) of torque with unrestricted practicality for everyday use and leisure.</description></car></cars>"})
    ).done(
        function(data1, data2, data3)
        {
            var allData = [].concat(data1).concat(data2).concat(data3);
            $(allData).find("car").each(function(){
                $('#cars-data').append('<li><a href="#cars"><img src="' + $(this).find('pic').text() + '" title="sample" height="100%" width="100%"/><h3>Car type:<span> '+ $(this).find('name').text() +'</span></h3><p>' + $(this).find('description').text() + '</p></a></li>'); 
            })
            $('#cars-data').listview('refresh');            
        }
    ).fail(
        function()
        {
            alert('Fail');  
        }
    );

    function parseXML(data)
    {
        return $.ajax({
            type: "POST",
            url: "/echo/xml/",
            dataType: "xml",
            data: data,
            success: function(data) {}
        });
    }   
});

Explanation

Basically to understand this you only need to understand method $.when. It accepts multiple functions a parameters and provide done and fail callback. If we are reading same kind of data we can use concat function to merge everything together. After that do whatever you want with that data.

Gajotres
  • 57,309
  • 16
  • 102
  • 130
0

Yes you can just launch two ajax requests:

$.ajax({
    url: 'test.xml',
    type: "get",
    context: this,
    success: function (data) {
        alert("success");
    },
    error: function () {
        alert("failure");
    }
});
$.ajax({
    url: 'test2.xml',
    type: "get",
    context: this,
    success: function (data) {
        alert("success");
    },
    error: function () {
        alert("failure");
    }
});

See question jQuery: Making simultaneous ajax requests, is it possible? if you wants more information

Extract of answers :

" You want asynchronous (which is the default). The browser limits you to two requests at any given time. This is part of the HTTP specification. (HTTP 1.1 specification, section 8.1.4) "

Community
  • 1
  • 1
Corinne Kubler
  • 2,072
  • 5
  • 22
  • 34