-2

Why I can't alert x value after this ajax request? I work with error function.

           var x;

    $.ajax({
        url: 'http://gdata.youtube.com/feeds/api/videos/' + code,
        type: 'get',
        dataType: 'json',
        success: function(data) { 
           console.log('Result 1: ');
           console.log(data.responseText);
        },
        error: function(e) {
            var text = e.responseText;

            parser = new DOMParser();
            xmlDoc = parser.parseFromString(text, "text/xml");

            var titleXml = xmlDoc.getElementsByTagName('title')[0];
            var contentXml = xmlDoc.getElementsByTagName('content')[0];

            title = titleXml.childNodes[0];
            content = contentXml.childNodes[0];

            x = title;

        }
    });

    alert(x);

if I make alert local in error function everything work fine, but I need to alert x value after ajax.

  • 1
    You can't cause the ajax call is asynchronies – Philip G Nov 18 '13 at 12:19
  • not without changing the ajax settings, i.e. not by doing the call as the question was asked. In his question the call is made asynchronous. Thats why he can't! – Philip G Nov 18 '13 at 12:25

3 Answers3

2
   $.ajax({
        url: 'http://gdata.youtube.com/feeds/api/videos/' + code,
        type: 'get',
        dataType: 'json',
        success: function(data) { 
           console.log('Result 1: ');
           console.log(data.responseText);
            alert(x);
//          ^^^^^^^^when get any success then use alert on ajax success
        },
        error: function(e) {
            var text = e.responseText;

            parser = new DOMParser();
            xmlDoc = parser.parseFromString(text, "text/xml");

            var titleXml = xmlDoc.getElementsByTagName('title')[0];
            var contentXml = xmlDoc.getElementsByTagName('content')[0];

            title = titleXml.childNodes[0];
            content = contentXml.childNodes[0];

            x = title;
            alert(x); 
//          ^^^^^^^^when get any error then use alert on ajax error  

        }
    });
Paul S.
  • 64,864
  • 9
  • 122
  • 138
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
1

The error or success functions are called in case of error or success of Ajax Request when it completes so the code that assigns the value to x would not have executed if you alert the value outside the functions.

The value of x is assigned after the request is completed so it works in case you have it alerted inside the error function.

0

Try this code please :

           var x;

$.ajax({
    url: 'http://gdata.youtube.com/feeds/api/videos/' + code,
    type: 'get',
    dataType: 'json',
    success: function(data) { 
       x = 0;
       console.log('Result 1: ');
       console.log(data.responseText);
    },
    error: function(e) {
        var text = e.responseText;

        parser = new DOMParser();
        xmlDoc = parser.parseFromString(text, "text/xml");

        var titleXml = xmlDoc.getElementsByTagName('title')[0];
        var contentXml = xmlDoc.getElementsByTagName('content')[0];

        title = titleXml.childNodes[0];
        content = contentXml.childNodes[0];

        x = title;

    }
});

alert(x);

What is your error ?