更新Javascript全局变量 [英] update Javascript Global Variable
问题描述
我在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 functionBindNewSalesAttribute
; you do it in the functionsuccess
. When your functionBindNewSalesAttribute
returns, the function objectsuccess
is created, but not yet called, so the variablevarBindNewSalesTranGrid
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 functionsuccess
of your
这篇关于更新Javascript全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!