今天遇到的一個小坑,js傳參 轉義符(\)的問題
阿新 • • 發佈:2019-01-22
今天改了一個以前人員的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");
}
})
謝謝各種坑,讓我成長