将包含多个变量的 URL 作为参数发送到另一个页面

发布于 2024-12-05 02:21:41 字数 982 浏览 1 评论 0原文

我有一个 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['data'];) 执行 GET,我会得到以下结果:

http://myserver.com//game.php?type=loadgame

我想要得到的是:

http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704

有什么想法吗? :-S

I have a Javascript page, that sends data to a PHP page. That data is a URL with different querystrings, for example:

    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);
        }
    });

The number of querystring variables can vary, so I can't send it with the data parameter of the ajax function. If I do a GET of the data variable ($_GET['data'];) I get this result:

http://myserver.com//game.php?type=loadgame

and what I'd like to get is:

http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704

Any idea? :-S

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

韵柒 2024-12-12 02:21:41

你必须转义才能获得有效的网址:

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;

You gotta escape to get a valid url:

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;
又怨 2024-12-12 02:21:41

您缺少的是 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中构造localURL,请使用escape()函数

What you are missing, is URL escaping in localUrl varaible.

It should be like this:

var localURL = "http://localhost/app/proxy.php?data=http%3A%2F%2Fmyserver.com%2Fgame.php%3Ftype%3Dloadgame%26userInfoName%3DAA%26userPwd%3DAA%26nocache%3D0.8046834595784704"

if you construct the localURL in javascript, use escape() function

迷乱花海 2024-12-12 02:21:41
var localURL = "http://localhost/app/proxy.php?data=" + encodeURIComponent("http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704");
var localURL = "http://localhost/app/proxy.php?data=" + encodeURIComponent("http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704");
夕色琉璃 2024-12-12 02:21:41

这就是浏览器看到的:

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')

This is what the browser sees:

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

so you should escape the string "http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704" like this

var data=$.URLEncode('http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704')
冷心人i 2024-12-12 02:21:41

您需要对保留字符进行编码。如果您像这样重组,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);
    }
});

将请求:

http://localhost/app/proxy.php?data=http%3A%2F%2Fmyserver.com%2Fgame.php%3Ftype%3Dloadgame%26userInfoName%3DAA%26userPwd%3DAA%26nocache%3D0。 8046834595784704

You need to encode the reserved characters. jQuery can do this for your automatically if you restructure like so:

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);
    }
});

Will request for:

http://localhost/app/proxy.php?data=http%3A%2F%2Fmyserver.com%2Fgame.php%3Ftype%3Dloadgame%26userInfoName%3DAA%26userPwd%3DAA%26nocache%3D0.8046834595784704

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文