更新Javascript全局变量 [英] update Javascript Global Variable

查看:66
本文介绍了更新Javascript全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net开发工作,我有两个文件,即

i)UserControl

ii).js(javascript)



在.js文件以下是代码



  var  varBindNewSalesTranGrid = ' '; 
function BindNewSalesAttribute(){
$ .ajax({
type: POST
url: / weblthod的url返回字符串
data:' {}'
contentType: application / json; charset = utf-8
dataType: json
成功: function (data){
varBindNewSalesTranGrid + = data.d;
},
error: function ( xhr,ajaxOptions,thrownError){
}
});
}



在Usercontrol文件中



 $(< span class =code-sdkkeyword> document )。ready( function (){
BindNewSalesAttribute();
alert( varBindNewSalesTranGrid);
});
// 上面引用的文件.js文件



当我运行我的usercontrol文件时,我看到varBindNewSalesTranGrid的值为空('').Function返回值'TestString',我在BindNewSalesAttribute函数中赋值varBindNewSalesTranGrid,但仍然没有更新值。

如果您有任何想法,请告诉我。

解决方案

.ajax({
type: POST
url: / url返回字符串
data:' {}'
contentType: application / json; charset = utf-8
dataType: json
成功:< span class =code-keyword> function (data){
varBindNewSalesTranGrid + = data.d;
},
error: function (xhr,ajaxOptions,thrownError){
}
});
}



在Usercontrol文件中



 

document )。ready( function (){
BindNewSalesAttribute();
alert(varBindNewSalesTranGrid);
});
// 上面引用的文件.js文件



当我运行我的usercontrol文件时,我看到varBindNewSalesTranGrid的值为空('').Function返回值'TestString',我在BindNewSalesAttribute函数中赋值varBindNewSalesTranGrid,但仍然没有更新值。

如果您有任何想法,请告诉我。


这只是异步操作的顺序。您不在函数中更新变量 BindNewSalesAttribute ;你可以在函数 success 中完成。当函数 BindNewSalesAttribute 返回时,函数对象 success 已创建,但尚未调用,因此变量 varBindNewSalesTranGrid 尚未更新。当HTTP请求发送到服务器端时,它将被更新,服务器端处理它并发送HTTP响应,通过调用函数 success 在客户端处理你的


I am working in asp.net development, I have Two files i.e.
i) UserControl
ii).js (javascript)

In .js File following is code

var varBindNewSalesTranGrid = '';
function BindNewSalesAttribute() {
   $.ajax({
      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 () {
    BindNewSalesAttribute();
    alert(varBindNewSalesTranGrid);
});
//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.

解决方案

.ajax({ 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 () { BindNewSalesAttribute(); alert(varBindNewSalesTranGrid); }); //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.


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


这篇关于更新Javascript全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆