JS this作用域以及GET傳輸值過長的問題解決方法
更新時間:2013年08月06日 16:20:45 作者:
專IE7瀏覽器,IE URL參數(shù)過長問題,引發(fā)HTTP Status 122報錯;this作用域問題,對應(yīng)的解決方法如下,感興趣的朋友可以參考下,希望對大家有所幫助
在開發(fā)項目的時候,前端遇到兩個比較隱蔽的問題。
問題一.專IE7瀏覽器,IE URL參數(shù)過長問題,引發(fā)HTTP Status 122報錯
原因:在IE6.8下沒有什么問題,但在IE7就不兼容get參數(shù)過長,google上說“Don't use the GET method in Ajax Apps, if you can void it, because IE7 craps out with more than 2032 characters in a get string”
解決方法:
把原項目采用jsonp get的數(shù)據(jù)方法改為 常規(guī)post數(shù)據(jù)方法
問題二. this作用域問題
原因:this如果不是在對象內(nèi)部默認為是 window這個大對象,如下面的this如是放在一個ajax的里面指的是當前域名ajax對象
解決方法:
var test={};
test.getflash = 2;
test.test =function(){
alert(this.getflash); //2
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert(this.getflash); //等于undefine
}
});
}
解決方法:
test.test =function(){
var thisValue = this;
alert(thisValue.getflash); //2
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert(thisValue.getflash); //2
}
});
}
問題一.專IE7瀏覽器,IE URL參數(shù)過長問題,引發(fā)HTTP Status 122報錯
原因:在IE6.8下沒有什么問題,但在IE7就不兼容get參數(shù)過長,google上說“Don't use the GET method in Ajax Apps, if you can void it, because IE7 craps out with more than 2032 characters in a get string”
解決方法:
把原項目采用jsonp get的數(shù)據(jù)方法改為 常規(guī)post數(shù)據(jù)方法
問題二. this作用域問題
原因:this如果不是在對象內(nèi)部默認為是 window這個大對象,如下面的this如是放在一個ajax的里面指的是當前域名ajax對象
解決方法:
復(fù)制代碼 代碼如下:
var test={};
test.getflash = 2;
test.test =function(){
alert(this.getflash); //2
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert(this.getflash); //等于undefine
}
});
}
解決方法:
復(fù)制代碼 代碼如下:
test.test =function(){
var thisValue = this;
alert(thisValue.getflash); //2
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert(thisValue.getflash); //2
}
});
}
相關(guān)文章
JS解決?Array.fill()參數(shù)為對象指向同一個引用地址的問題
這篇文章主要介紹了JS解決?Array.fill()參數(shù)為對象指向同一個引用地址問題,解決方案使用map返回出不同的引用的地址,fill參數(shù)可隨意填寫(不為空),主要是map函數(shù)中返回的數(shù)據(jù),需要的朋友可以參考下2023-02-02
Javascript 實現(xiàn)的數(shù)獨解題算法網(wǎng)頁實例
此算法的實現(xiàn),就是模擬人腦的思考和計算過程,有需要的朋友可以參考一下2013-10-10

