1

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

1
  • Well you're sending it to proxy.php and the URL is using game.php; are you sure proxy.php is correctly forwarding the query string? Commented Sep 16, 2011 at 9:50

5 Answers 5

3

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
var localURL = "http://localhost/app/proxy.php?data=" + encodeURIComponent("http://myserver.com/game.php?type=loadgame&userInfoName=AA&userPwd=AA&nocache=0.8046834595784704");

Comments

0

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

1 Comment

It's worth updating your answer to say a plugin is required for $.URLEncode to work.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.