Sending a URL with multiple variables as a parameter to another page
我有一个 Javascript 页面,它将数据发送到一个 PHP 页面。该数据是具有不同查询字符串的 URL,例如:
1 2 3 4 5 6 7 8 9 10 11 | var localURL ="http://localhost/app/proxy.php?data=http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704" $.ajax({ url: localURL, beforeSend: function (xhr) { alert('beforesend'); }, success: function (data) { alert('success: ' + data); } }); |
查询字符串变量的数量可以变化,所以我不能用ajax函数的data参数发送。如果我对数据变量 ($_GET[\\'data\\'];) 执行 GET,我会得到以下结果:
http://myserver.com//game.php?type=loadgame
而我想得到的是:
http://myserver.com/game.php?type=loadgame
你必须逃跑才能得到一个有效的 url:
1 2 | var data = escape('http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704'); var localURL ="http://localhost/app/proxy.php?data=" . data; |
您缺少的是
应该是这样的:
1 | var localURL ="http://localhost/app/proxy.php?data=http%3A%2F%2Fmyserver.com%2Fgame.php%3Ftype%3Dloadgame%26userInfoName%3DAA%26userPwd%3DAA%26nocache%3D0.8046834595784704" |
如果你用javascript构造localURL,使用
n
这是浏览器看到的:
1 2 3 4 5 6 7 8 9 | http://localhost/app/proxy.php? data=http://myserver.com/game.php?type=loadgame => param 1 &userInfoName=AA => param 2 &userPwd=AA => param 3 &nocache=0.8046834595784704 => param 4 |
所以你应该转义字符串"http://myserver.com/game.php?type=loadgame
1 | var localURL ="http://localhost/app/proxy.php?data=" + encodeURIComponent("http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704"); |