0

I am making a website in Jquery. Here I m trying to parse an xml. I've got one file m_xml.js who get my xml and the c_parseXml.js who will parse it. But I can't get the file to the parse.js file look :

function test() {
    RameneXml(function(output) {
        return output;
    });
}

function RameneXml(handleData) {
    $.ajax({
        type: "GET",
        url: "web/xml/calendrier.xml",
        dataType: "xml",
        async: "false",
        success: function(xml) {
            handleData(xml);
        }
    });
}

It's my m_xml file. and with this file I want to get it in a variable. look my file parseXml.js

function DonnerDiffusion() {
    output = test();
    console.log(output);
}

but in the console I can see undefined

What have I do Wrong ? Thanks

SalindaKrish
  • 149
  • 1
  • 3
  • 12
Blinkix
  • 47
  • 1
  • 9
  • load jquery and then put your code inside ready function. – Hikmat Sijapati Jan 06 '17 at 08:48
  • Your `test()` function doesn't return anything, hence `undefined`. Also, remove `async: false`. It's extremely bad practice to use it. – Rory McCrossan Jan 06 '17 at 08:49
  • what ? but I wrote return output ? Sorry I beging jQuery. But the return statement do not apply ? – Blinkix Jan 06 '17 at 08:49
  • Yes - in the anonymous function you provide to `RameneXml()`. Nothing is returned from `test()` – Rory McCrossan Jan 06 '17 at 08:50
  • so what should I do if I want to return something from test() ? – Blinkix Jan 06 '17 at 08:52
  • 1
    I don't see the point of your `test()` function. It just wraps `RameneXml()`, so why not call that instead, and put your logic that deals with the XML response in the callback? – Rory McCrossan Jan 06 '17 at 08:52
  • See the question I marked as duplicate for more information. You've already solved the problem once in your code by passing the callback function to `RameneXml()`. You need to follow the same pattern for `test()` - or remove it completely as it seems pretty redundant at the moment. – Rory McCrossan Jan 06 '17 at 08:54
  • yes, because I have understand than I can't do a return in my ajax function – Blinkix Jan 06 '17 at 08:54
  • So how could I call it on the parseXml.js if I can return anything – Blinkix Jan 06 '17 at 08:56
  • $Blinkix: what do u mean by the `output = test();` in **DonnerDiffusion() ** and you have used it as a parameter in RameneXml()? I think you don't need recursive methods to do this – Kalanka Jan 06 '17 at 09:07
  • in fact I would like to have a var exemple output that contains my xml. And here I print it in the console to see if it work – Blinkix Jan 06 '17 at 09:10
  • Make thhe funtion DonnerDiffusionas`function DonnerDiffusion() { var output = test(); console.log(output); }` mention what you want to archieve clearly... if you want to run the function after ajax call use `$( document ).ajaxComplete(function() { $( ".log" ).text( "Triggered ajaxComplete handler." ); });` – SalindaKrish Jan 06 '17 at 09:15
  • I'm not sure I clearly understand what you want to say ... sorry – Blinkix Jan 06 '17 at 09:27
  • As I have understood you need to pass result of one ajax function data to another function isn't it? Am I right? – SalindaKrish Jan 06 '17 at 09:29
  • yes and this other function is on another page – Blinkix Jan 06 '17 at 09:30
  • So I think what you need to do is pass variable from one js to another js? [have a look here](http://stackoverflow.com/questions/29686004/how-can-i-pass-a-variable-from-one-js-file-to-another-js) – SalindaKrish Jan 06 '17 at 09:39
  • thanks for your help but I finally change some thing and I've done it on js Thanks a lot – Blinkix Jan 06 '17 at 09:43

0 Answers0