1

I am wanting to write some code to return some data from an aJax call.

Here is the resource that I am referring to: http://www.w3schools.com/jquery/ajax_ajax.asp

Here is the code that I have written:

var data = getData("http://www.file.txt", function(result));
alert(data);

function getData(dataUrl, result)
{
    $.ajax({url: dataUrl, success: function(result){
        return result;
    }});
}

I am getting the following error:

Uncaught SyntaxError: Unexpected token )

At this line of code:

var data = getData("http://www.file.txt", function(result));

Also, is the above code efficient when getting large amounts of data?

Thanks

Simon
  • 7,991
  • 21
  • 83
  • 163
  • Uh, `function(result)` is the start of a function expression, but it's missing its body. The body [in which the `alert(result)` should go](http://stackoverflow.com/q/14220321/1048572). Also that `return result` in your `success` callback doesn't exactly work. – Bergi Jun 24 '15 at 05:43
  • 3
    Can you explain what you expect `function(result)` to do? – Felix Kling Jun 24 '15 at 05:45
  • 1
    I'm tempted to close this as duplicate of http://stackoverflow.com/q/14220321/218196 (because of the first line), but I feel like your actual problem is lack of understand how callbacks (and maybe even functions) work in JS. – Felix Kling Jun 24 '15 at 05:49

4 Answers4

0

This is not the correct syntax for a callback, plus the asynchronous call doesn't quite work like that. Try this instead - data is set in the callback.

var data;
getData("http://www.file.txt"); 

function getData(dataUrl)
{
    $.ajax({url: dataUrl, success: function(result){
        data = result;
        alert(data);            
    }});
}
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
  • Why that global `var data` variable? – Bergi Jun 24 '15 at 05:45
  • `data` is just not quite global, just in whatever closure this fragment is in. `result` parameter because I missed it ;) – Evan Knowles Jun 24 '15 at 05:46
  • Sure, it's free ("global") to `getData`. Asynchronous calls don't work like that. There's no reason to have it in that scope (whichever scope that is). – Bergi Jun 24 '15 at 05:48
0

From JavaScript basics, a function should satisfy following.

  1. A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
  2. A function is basically a JavaScript object. So, The parentheses are required,as they set apart a function from the other items of your script or web page.

So the line of code is actually incorrect.

var data = getData("http://www.file.txt", function(result));

It should be either of the below.

var data = getData("http://www.file.txt", function(result) {
    //anonymous function body
});

or

var data = getData("http://www.file.txt", callbackFn(result));

function callbackFn(result) {
    //function body goes here.
}
Adersh M
  • 596
  • 3
  • 19
-1

You are not passing brackets so you get Uncaught SyntaxError: Unexpected token) error

var data = getData("http://www.file.txt", function(result){});

But I think you have to use function like this

function getData() {
  return Promise(function(resolve, reject) {
    $.ajax({
      // ...
      success: function(data) {
        resolve(data);
      },
      error: function(jqxhr, status, error) {
        reject(error);
      }
    });
  });
}

getData("http://www.file.txt").then(function(result) {
 console.log(result);
}).catch(function() {
  // an error occurred
});

Keval Bhatt
  • 6,224
  • 2
  • 24
  • 40
  • it is just an example why op get error so i given explanation @bergi – Keval Bhatt Jun 24 '15 at 05:49
  • 1
    But your example will throw an error because `result` is not defined. I don't think you are aware of what `callback(result)` in `var data = getData("http://www.file.txt",callback(result));` does. – Felix Kling Jun 24 '15 at 05:51
  • op pass result in his function so i passed it and this solution is for Uncaught SyntaxError: Unexpected token ) so i given explanation on it not for result and other stuff op asked for Uncaught SyntaxError: Unexpected token ) – Keval Bhatt Jun 24 '15 at 05:55
  • oh yes you are right about Promise issue so i edited my answer so if any one refers it will get proper solution not wrong one thanks @FelixKling – Keval Bhatt Jun 24 '15 at 06:32
  • @KevalBhatt: Now that's the [promise constructor antipattern](http://stackoverflow.com/q/23803743/1048572) :-/ Use `return Promise.resolve($.ajax(…))` (or don't cast to native promises at all) – Bergi Jun 24 '15 at 08:18
-1

If you are using ajax call, then you must use success or .then with it to be executed as callback after receiving data.

The code should be only this:

var data = '';
$.ajax({url: dataUrl, success: function(result){
        data = result;
        alert(data);
    }});
Nikhil Batra
  • 3,118
  • 14
  • 19
  • Can you explain what the difference between your code and the OP's code is? – Felix Kling Jun 24 '15 at 05:46
  • As per my understanding of the question, what is needed is that he wants to execute some code as callback and assign the value to data. So as a result of success function, the result will be assigned to data. – Nikhil Batra Jun 24 '15 at 05:49
  • OK, to be specific: `alert(data)` will alert the empty string, not the expected result. – Bergi Jun 24 '15 at 05:53
  • alert(data) will be used after data is assigned result value. – Nikhil Batra Jun 24 '15 at 05:54
  • Why did you place it in there (the OP did not)? Why use that `data` variable at all instead of just `alert(result)`? – Bergi Jun 24 '15 at 05:56
  • The results stored in data variable can be used outside the ajax function as well, while the scope of result is limited, thus I assigned result to data. I may not be correct but this was what I understood of the question. – Nikhil Batra Jun 24 '15 at 05:58
  • But if you placed the `alert(data)` outside of the ajax function callback, it wouldn't work, so apparently we cannot use the variable there. – Bergi Jun 24 '15 at 06:02
  • Yes what you are saying is absolutely right and I know that too, but since there is no clear demarcation regarding the needs of the question, I alerted the received data. This was all the purpose of using that. – Nikhil Batra Jun 24 '15 at 06:05