Click here to Skip to main content
15,917,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am working in development, I have Two files i.e.
i) UserControl
ii).js (javascript)

In .js File following is code

var varBindNewSalesTranGrid = '';
function BindNewSalesAttribute() {
      type: "POST",
      url: "/url of Webmthod returns string",
      data: '{}',
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          varBindNewSalesTranGrid += data.d;
      error: function (xhr, ajaxOptions, thrownError) {

In Usercontrol file

$(document).ready(function () {
//Which is file referenced above .js file

When I run my usercontrol file I see the value of varBindNewSalesTranGrid is empty ('').Function returns value 'TestString' and I am assigning value varBindNewSalesTranGrid in BindNewSalesAttribute function but still value is not getting updated.
if you have any ideas then let me know.
Updated 13-Oct-14 5:11am
Sergey Alexandrovich Kryukov 13-Oct-14 11:33am    
I formatted the code samples in your post. Please click Improve question to see how it should be done, do it properly next time.

1 solution

This is just the sequence of asynchronous operations. You don't update your variable in your function BindNewSalesAttribute; you do it in the function success. When your function BindNewSalesAttribute returns, the function object success is created, but not yet called, so the variable varBindNewSalesTranGrid is not yet updated. It will be updated when HTTP request is sent to the server side, the server side process it and sends HTTP response, which is handled on your client side by calling the function success of your $.ajax object.

To check it up, just call your alert in your success function, but also do some visually noticeable in your error function, in case the HTTP request is unsuccessful.

By the way, here you can observe the effect of closure. Please see:[^],[^].

What to do? Redesign your code to avoid using an assumption of the order of calls of the functions in question. Avoid using outer-scope variables, which are best avoided. You need to abstract out your success/error actions from your library function BindNewSalesAttribute. Say, you can do it by adding a couple of function parameters (along with some other parameters).

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900