1
<input type="text" id="basla" value="some" />


$.get('@Url.Action("BaslangicGetir", "Deneme")' + '?id=' + fisid, function (gelen) {
            $("#basla").val(gelen);
        });

and after get value

var baslangic = $("#basla").val();
alert(baslangic);

alert "some" but value is not some

1
  • 3
    Take into account that get works in async mode, if you place var baslangic = $("#basla").val(); alert(baslangic); in the following line of get statement, you'll get old value Commented Jul 1, 2013 at 12:53

1 Answer 1

1

No, the value is "some":

<input type="text" id="basla" value="some" />

When the AJAX call is complete, the value will be changed to another value (which may or may not be "some", we can't tell from this code):

$.get('@Url.Action("BaslangicGetir", "Deneme")' + '?id=' + fisid, function (gelen) {
    $("#basla").val(gelen);
});

However, the "A" in AJAX stands for "asynchronous" because this happens asynchronously. It's possible that it will take place before a later sequential statement is executed, but not guaranteed. And if that statement is immediately following the AJAX request, it's very unlikely. So if this is the complete code:

$.get('@Url.Action("BaslangicGetir", "Deneme")' + '?id=' + fisid, function (gelen) {
    $("#basla").val(gelen);
});
var baslangic = $("#basla").val();
alert(baslangic);

Then the order of events will be:

  • Initiate the GET request to the server.
  • Store the current value ("some") in a variable.
  • Alert that variable.
  • [some more milliseconds pass, possibly executing later code not shown here]
  • Receive the response from the GET request.
  • Set the value to the new response from the server.

In order to respond to the new value from the GET request, you have to do it in the callback function for that request. Since that function is guaranteed to execute after the request completes. So the correct sequence for your statements would be:

$.get('@Url.Action("BaslangicGetir", "Deneme")' + '?id=' + fisid, function (gelen) {
    $("#basla").val(gelen);
    var baslangic = $("#basla").val();
    alert(baslangic);
});

Edit: (based on your comment)

If you have multiple AJAX requests they will all run asynchronously. And there is no guarantee in which order they will complete. So if you need to wait for three requests to complete then one approach could be to maintain some flags as to the state of the requests. Maybe something like this:

var completedRequests = 0;

var handleFinalResponse = function() {
    if (completedRequests >= 3) {
        // do whatever you need here after all three are complete
    }
};

$.get(someURL1, function (response) {
    // do something with the response
    completedRequests++;
    handleFinalResponse();
});

$.get(someURL2, function (response) {
    // do something with the response
    completedRequests++;
    handleFinalResponse();
});

$.get(someURL3, function (response) {
    // do something with the response
    completedRequests++;
    handleFinalResponse();
});

I don't think there's a race condition here, but if someone can find one please point it out so we can fix it. Essentially what you're doing is storing a global value of how many responses you've received from the server. So whichever AJAX response executes the third time should result in executing the code inside the conditional in response to all three being completed.

If this doesn't work right (I don't have a lot of experience with synchronizing threads, so I may have overlooked a glaring error in that code) then another option might be to chain the requests so that each one isn't sent until the previous one completes. This approach is slower, but simpler:

$.get(someURL1, function(response1) {
    // handle response1
    $.get(someURL2, function(response2) {
        // handle response2
        $.get(someURL3, function(response3) {
            // handle response3
        });
    });
});

To prevent excessive indentation you can define the callback functions elsewhere and just reference them here.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for answer but ı have three get and after use three value in function ?
@KaanÇolak: Assuming I understand what you mean, I've updated the answer with some ideas. Basically you're looking at multiple asynchronous calls, and you have to synchronize the responses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.