我有一个Javascript页面,它将数据发送到PHP页面。该数据是具有不同查询字符串的URL,例如:
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($ _GET ['data'];),我得到这个结果:
http://myserver.com//game.php?type=loadgame
我想得到的是:
http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704
有什么想法吗? :-S
答案 0 :(得分:3)
你必须逃避才能得到一个有效的网址:
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 :(得分:1)
你缺少的是localUrl
变量中的URL转义。它应该是这样的:
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中构建本地网址,请使用escape()
函数
答案 2 :(得分:0)
var localURL = "http://localhost/app/proxy.php?data=" + encodeURIComponent("http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704");
答案 3 :(得分:0)
这是浏览器看到的内容:
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&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704”
var data=$.URLEncode('http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704')
答案 4 :(得分:0)
您需要对保留字符进行编码。如果您重新构建,jQuery可以自动执行此操作:
var localURL = "http://localhost/app/proxy.php";
var getString = "http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704";
$.ajax({
type: 'GET',
data: { data: getString },
url: localURL,
beforeSend: function (xhr) {
alert('beforesend');
},
success: function (data) {
alert('success: ' + data);
}
});
请求: