JS this作用域以及GET傳輸值過長的問題解決方法
更新時間:2013年08月06日 16:20:45 作者:
專IE7瀏覽器,IE URL參數過長問題,引發(fā)HTTP Status 122報錯;this作用域問題,對應的解決方法如下,感興趣的朋友可以參考下,希望對大家有所幫助
在開發(fā)項目的時候,前端遇到兩個比較隱蔽的問題。
問題一.專IE7瀏覽器,IE URL參數過長問題,引發(fā)HTTP Status 122報錯
原因:在IE6.8下沒有什么問題,但在IE7就不兼容get參數過長,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的數據方法改為 常規(guī)post數據方法
問題二. this作用域問題
原因:this如果不是在對象內部默認為是 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參數過長問題,引發(fā)HTTP Status 122報錯
原因:在IE6.8下沒有什么問題,但在IE7就不兼容get參數過長,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的數據方法改為 常規(guī)post數據方法
問題二. this作用域問題
原因:this如果不是在對象內部默認為是 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-09