0

After reading various examples on stackoverflow I wrote this function :

function showGetResult(crossDomainUrl) {
    $.ajax({
        url: crossDomainUrl,
        type : 'GET',
        crossDomain: true,
        success: function (data) {
            debug(data);
            return data;
        }
    });
}

and called it using this

alert(showGetResult(crossDomainUrl));

But all I get is 'undefined', this is being used in a web-browser extension inside a content-script.

Stacked
  • 841
  • 2
  • 12
  • 23
  • exact duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Bergi Oct 03 '13 at 14:18
  • possible duplicate of [How to return AJAX response Text?](http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text) – Quentin Oct 03 '13 at 14:18
  • Although similar both does not address my problem like numerous other examples here, see my comment - "Thanks this works, but I need to grab a javascript file. I tried the datatype values jsonp, script and html and all failed. see - jsfiddle.net/tsyGj" – Stacked Oct 04 '13 at 02:13
  • @Stacked — You have two different problems. The question this is marked as a duplicate of solves the problem of returning the data. Your other problem (which we can only tell from your JSFiddle, and not from any code in your question) is the Same Origin Policy, and that is covered by [this other duplicate question](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy). – Quentin Oct 04 '13 at 11:38
  • @Quentin Should I delete this? – Stacked Oct 04 '13 at 13:32

2 Answers2

0

This is because the Ajax request runs asynchronously. The return data doesn't do anything. You could change it to (updated to reflect the request in the comments to be able to download a script):

function showGetResult(crossDomainUrl) {
    return $.ajax({
        url: crossDomainUrl,
        type : 'GET',
        dataType: 'script',
        crossDomain: true
     });
}

showGetResult('http://code.jquery.com/ui/1.10.3/jquery-ui.js')
    .done(function(data) {
        alert("success: " + data);
    })
    .fail(function(jqXHR, textStatus, ex) {
        alert("failed: " + textStatus);
    });

For the call actually to work cross-domain, you will need to use jsonp or script. Read this wiki for more information about Same-origin policy. Refer to this answer for more information about using jsonp.

The code above will inject the downloaded jscript in the dom and execute it.

Community
  • 1
  • 1
mhu
  • 17,720
  • 10
  • 62
  • 93
  • I tested this in my code but no alert is getting fired, same with http://jsfiddle.net/UmxRr/ ? – Stacked Oct 03 '13 at 12:24
  • As in your original code, only success was handled. I've added the fail handler, see http://jsfiddle.net/UmxRr/2/ – mhu Oct 03 '13 at 14:22
  • But why is it failing in the first place ? – Stacked Oct 04 '13 at 02:06
  • Updated the answer for downloading scripts, as you requested. – mhu Oct 04 '13 at 11:36
  • 1
    still gives ```success: undefined```. – Stacked Oct 04 '13 at 13:34
  • That's correct. No value is returned, but "The code above will inject the downloaded jscript in the dom and execute it." – mhu Oct 05 '13 at 18:02
  • jQuery doesn't use Ajax for downloading cross-domain scripts. It injects a ` – mhu Oct 06 '13 at 18:27
-1

The $.ajax() set up the query, and return immediately, so the function returns before the request is completed. Specify a function to call on completion of the query using success.

function showGetResult(crossDomainUrl) {
    $.ajax({
        url: crossDomainUrl,
        type : 'GET',
        crossDomain: true,
        success: showData
    });
}

function showData(data){
    debug(data);
    return data;
}
showGetResult(crossDomainUrl);

see http://jsfiddle.net/5J66u/8/ - (updated to specify jsonp and a better URL for it)

Matt
  • 364
  • 4
  • 10
  • Thanks this works, but I need to grab a javascript file. I tried the datatype values jsonp, script and html and all failed. see - http://jsfiddle.net/tsyGj/ – Stacked Oct 03 '13 at 13:44
  • The `showGetResult` function will still return `undefined`, it doesn't matter that `showData` returns. – Quentin Oct 04 '13 at 11:36