JS this作用域以及GET传输值过长的问题解决方法

(编辑:jimmy 日期: 2024/10/10 浏览:2)

在开发项目的时候,前端遇到两个比较隐蔽的问题。

问题一.专IE7浏览器,IE URL参数过长问题,引发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的数据方法改为 常规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
}
});
}

一句话新闻

高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。