今天改了一个以前人员的bug问题,就是传参数时候出现\的问题
例如传的参数是http://.*?.xx.com/\d+/\d+/\d+/.*?.html.*这样一个正则
使用jquery的ajax如下:
$.ajax({
type: "POST",
url: "./index.php?xxx",
data: "host="+strHost,
dataType: "json",
success:function(r){
alert("ok");
}error:function(r){
alert("error");
}
})
传过去的参数strHost 就变成了这样:
http://.*?.xx.com/d+/d+/d+/.*?.html.*
原因在于data的格式问题,使用"host="+strHost;这种 \d 会被js自动转义
应当使用{"ss":ss}这样的传值方式,jquery的ajax会帮你把参数完整传到后台去
最后结果:
$.ajax({
type: "POST",
url: "./index.php?xxx",
data: {"host":strHost},
dataType: "json",
success:function(r){
alert("ok");
}error:function(r){
alert("error");
}
})
谢谢各种坑,让我成长