0

I have gone through many posts. Some say that after jquery 1.8, its deprecated. For some, it still works. How can I implement this

            $.ajaxSetup({async:false});
             duration_reco = $.ajax({
                   url : newurl, 
                   async : false,
                   dataType : 'json',

                });

            console.log(duration_reco.responseText);

It says durtaion_reco is undefined. I have checkeed to see that duration_reco cotaines valid data. the ajax request is executing synchronously.

user3392740
  • 445
  • 2
  • 7
  • 19
  • what does `newurl` contain ? – Amit Joki Mar 08 '14 at 15:02
  • it contains a url to retrieve data from youtube newurl="https://gdata.youtube.com/feeds/api/videos/" + ytid +"?v=2&alt=jsonc&callback=?"; I have checked to see it returns valid data – user3392740 Mar 08 '14 at 15:04
  • what actually is your question ? – Amit Joki Mar 08 '14 at 15:05
  • If you want to use async false you need to use a version of jQuery prior to 1.8. Either that, or you can use the preferred methods in newer versions - "As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success()." – Jay Blanchard Mar 08 '14 at 15:05
  • @JayBlanchard: Just because it is deprecated, doesn't mean it doesn't work. It's just not recommended. – Matt Mar 08 '14 at 15:06
  • I know @Matt, but if the OP performs another upgrade at some point and the code has been removed that supports async false, he will have problems. Better to use the preferred methods. – Jay Blanchard Mar 08 '14 at 15:07
  • FYI: As stipulated in doc, async false is deprecated when used with $.deferred object. As XHR object still support synchronous request, there is no reason jqXHR won't do the same. That's said cases of useful uses of any synchronous request are close to null... – A. Wolff Mar 08 '14 at 15:17

1 Answers1

1

$.ajax supports many different types of requests (which jQuery calls transports), and hides them from the user (normal XHR, XHR with CORS, JSONP). This is good as it means you can fire any URL at $.ajax(), and not have to worry about what is happening behind the scenes.

However, it is bad in that not all types of requests support all of the options $.ajax() supports. Things start to fail, seemingly randomly, when this happens. This is what is happening here.

Since you're requesting an resource from another domain, $.ajax() is making a JSONP request. To understand how JSONP works, see Can anyone explain what JSONP is, in layman terms?.

Basically, JSONP requests cannot happen synchronously. You can only perform them asynchronously, which involves altering your code to;

duration_reco = $.ajax({
    url : newurl, 
    dataType : 'json'
}).done(function (obj) {
    console.log(obj);
});

Synchronous AJAX requests are bad anyway. They lock up the browser. There is hardly ever a reason to use them. Avoid them at all costs. I cannot emphasise this enough.

You should also make sure your newurl variable starts with either http:// or https://; otherwise it'll be treated as a path on your current domain, and http://www.yourdomain.com/gdata.youtube.com/feeds/api... will be requested.

Community
  • 1
  • 1
Matt
  • 74,352
  • 26
  • 153
  • 180