1

I'd like to load some content from an URL, to use it in my code. I tried closures and this:

function getStringFromURL(url) {
  var getter = function() {
    this.result = "undef";
    this.func = function(response) {
      this.result = response;
    };
  };
  var x = new getter();
  $.get(url, x.func);
  return x.result;  // the it returns "undef" and not the wanted response
}

Nothing worked at all. I'll never got the content, but if I call it with alert like $.get("http://localhost:9000/x", function(response) { alert(response) }); it works -- but I'd like to save the response. I think theres a problem with the scope of the $.get-method.

Whats wrong with this?

Themerius
  • 1,861
  • 1
  • 19
  • 33

2 Answers2

3

You can't analyze the content obtained from another domain or port in a standard get query without an explicit accord given by the server.

Read this : https://developer.mozilla.org/en/http_access_control You'll see how to define the proper header for your site so that it says the browser that cross-domain requests are fine.

And you have a closure problem. Try this if you want to call x.func in another context than the getter :

var getter = function() {
   var _this = this;
   this.result = "undef";
   this.func = function(response) {
      _this.result = response;
     };
 };

EDIT : And as other have mentionned, you cannot return immediately x.result from getStringFromURL. You must use the value in the callback. In fact, it's more generally not possible to define a synchronous getter in javascript around an asynchronous call.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    Why works the code with `alert`? (`$.get("http://localhost:9000/x", function(response) { alert(response) });`) – Themerius May 27 '12 at 17:31
  • 1
    Because you're probably testing the website locally on your computer. So it's the same origin and you're allowed to process your own data. – chucktator May 27 '12 at 17:38
  • Some things are possible. Like alert, fill a div for display, console.log. Some aren't. – Denys Séguret May 27 '12 at 17:47
  • Sadly it's no closure-problem, "undef" is returned anyway -- and despite of the changes with the code-snippets from Beygi. – Themerius May 27 '12 at 18:04
  • 1
    The function returns before the asynchronous call calls the callback is neither a closure nor a cross domain issue (which would also fail in the alert version) – Rune FS May 27 '12 at 18:06
  • @RuneFS : The x.func function is passed as a callback to the $.get function. So there is no asynchronicity problem. – Denys Séguret May 27 '12 at 18:15
  • 2
    Sure there is since he's using the value set by the call back before the call back is called. Which is why the value is still the original value – Rune FS May 27 '12 at 21:17
  • Ah, you mean in the last line. Yes, I missed that and you're perfectly right. – Denys Séguret May 28 '12 at 05:49
1

$.get is Async method

you need to pass a callback function as an argument to getStringFromURL

function getStringFromURL(url, callback) {
            var getter = function () {
                this.result = "undef";
                this.func = function (response) {
                    this.result = response;
                    callback(response);
                };
            };
            var x = new getter();
            $.get(url, x.func);
        }

getStringFromURL("http://localhost:9000/x", function (res) { alert(res) });

if you want to return the result it is impossible.

You can't mix synchronous and asynchronous in JavaScript if you block the script, you block the Browser.

check it out here Asynchronous for cycle in JavaScript

Community
  • 1
  • 1
Beygi
  • 1,918
  • 1
  • 16
  • 22
  • This won't change the problems with the same origin policy. – chucktator May 27 '12 at 17:45
  • 1
    @chucktato there is no problem with origin policy here http://localhost:9000/ is his website and /x is the URL he wants to load as he mentioned in his question, he didn't say i want to load some content from a domain and i have a problem because when i load localhost it works. – Beygi May 27 '12 at 17:55
  • The `alert` prompts the correct answer from localhost, but "undef" is returned anyway. – Themerius May 27 '12 at 18:01
  • You shouldn't return but pass the value to the continuation (callback) – Rune FS May 27 '12 at 18:08
  • The question says: "I'd like to load some content from an URL". He stated that localhost works, but any other URL doesn't. The problem with the callback function is additional, as he gets the wrong output if he inserts an `alert()` too. – chucktator May 27 '12 at 18:11