-1

I have declared global variable before document.ready()

var getStatus=null;
    $(function () {

then in the document.ready function

beforeSubmit: function (postdata, formid) {
                     $.getJSON('@Url.Action("","")', { Id: postdata.Id},
                         function (data) {
                             getStatus = data;
                             alert(getStatus)// this give the correct status
                         });
                     alert(getStatus)//   but this one shows null
                     return [false];
                 },

I have tried using many ways but didn't get success.Please help

Mir Gulam Sarwar
  • 2,588
  • 2
  • 25
  • 39
  • 5
    AJAX is ***asynchronous***! Your "this one shows null" will actually run first. Before the AJAX call is done. A while later, after the function has already returned, when the AJAX call completes, that's when the callback will run and when the alert will show the right value. – gen_Eric May 27 '14 at 15:59
  • 1
    At the time of that second `alert()`, `$getJSON()` hasn't returned (or, probably, even run) yet. – Paul Roub May 27 '14 at 15:59
  • @janina: `$.getJSON` is shorthand for `$.ajax`. Meaning that `$.getJSON` is actually calling `$.ajax` internally. What you need to do is put everything involving `getStatus` in the callback. There's really no need to make that global variable, that's what callbacks/promises are for. – gen_Eric May 27 '14 at 16:03
  • @Rocket Hazmat may u please give a simple example or a link, I never used callback, I searched in google but didn't understand which one will work in my case – Mir Gulam Sarwar May 27 '14 at 17:21
  • @janina: The `function (data) {` you have in your code. That's the callback. That's where you can access the `data` and use it. – gen_Eric May 27 '14 at 17:26
  • @Rocket Hazmat but the problem is I need to change the value of getStatus in function(data), and then access getStatus in beforeSubmit function. But getStatus is not accessible in leter function,that's why I used global variable, but that doesn't seems to work. – Mir Gulam Sarwar May 27 '14 at 17:32
  • @janina: You can't do that. What are you trying to do in `beforeSubmit`? – gen_Eric May 27 '14 at 17:34
  • I am using jqgrid, if getStatus is true then it will submit data, if false the it will not submit – Mir Gulam Sarwar May 27 '14 at 17:36
  • beforeSubmit is a built in function of jqgrid, if it retrurn true then it will submit, else not – Mir Gulam Sarwar May 27 '14 at 17:39
  • @janina: You can try [@Martin Carney's solution](http://stackoverflow.com/a/23894621/206403). – gen_Eric May 27 '14 at 17:42
  • @Rocket Hazmat yes async:false works for me, Is there no possible way to make it asynchronous? – Mir Gulam Sarwar May 27 '14 at 17:44

2 Answers2

1

An ajax request is an asynchronous action, meaning it sends off a request, and when the server sends its reply, the success (or failure) method gets called. In your case, this means when $.getJSON is executed, it immediately executes the next line, that is, alert(getStatus). Later, the success function of getJSON will be executed, once it receives a response from the server.

If you want the browser to wait for the ajax call to finish before doing anything else (including execute the lines right after the ajax call), you need to use a regular ajax call (instead of getJSON) and add async: false to it, something like:

function (postdata, formid) {
    $.ajax({
        url: myUrl,
        dataType: 'json',
        async: false,
        data: myData,
        success: function(data) {
            getStatus = data;
            alert(getStatus);
        }
    });
    alert(getStatus);
    return [false];
}
Mar
  • 7,765
  • 9
  • 48
  • 82
  • 1
    While this works, `async: false` is not recommended as it will lock up the browser (you can't click on anything on the page) until the AJAX call is done. – gen_Eric May 27 '14 at 16:43
0

Ajax request is asynchronous action. when it is executed $.getJSON it immediately execute next line that is alert(getStatus) and at the same time success function of getJSON not executed. it will not wait for response to come from server.

So you will need to handle in success event only.

Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49