JS this作用域以及GET傳輸值過長的問題解決方法
更新時間:2013年08月06日 16:20:45 作者:
專IE7瀏覽器,IE URL參數(shù)過長問題,引發(fā)HTTP Status 122報錯;this作用域問題,對應的解決方法如下,感興趣的朋友可以參考下,希望對大家有所幫助
在開發(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對象
解決方法:
復制代碼 代碼如下:
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
}
});
}
相關文章
JS使用對象的defineProperty進行變量監(jiān)控操作示例
這篇文章主要介紹了JS使用對象的defineProperty進行變量監(jiān)控操作,結合實例形式分析了對象defineProperty方法的功能及簡單使用技巧,需要的朋友可以參考下2019-02-02使用JSON.parse將json字符串轉換成json對象的時候會出錯
使用JSON.parse將json字符串轉換成json對象的時候會出錯,主要是雙引號,回車換行等影響明顯,左尖括號和右尖括號也會導致顯示問題2014-09-09uniapp開發(fā)App出現(xiàn)網(wǎng)絡異常的解決方法
這篇文章主要給大家介紹了uniapp開發(fā)App出現(xiàn)網(wǎng)絡異常的解決方案,文中有相關的解決方法和步驟,具有一定的參考價值,需要的朋友可以參考下2023-09-09js實現(xiàn)json數(shù)據(jù)行到列的轉換的實例代碼
為了實現(xiàn)這樣的數(shù)據(jù)顯示出來三個序列,分別為鄭州、新鄉(xiāng)、安陽的電量,就需要自己實現(xiàn)對這樣數(shù)據(jù)的轉換,轉換成如下的形式:2013-08-08