0

I am trying to pass a text by parameter (with whitespaces) to the load function and it seems it doesn't work.

Currently i am doing this:

var text ="hello world this is an example";
$("#selector").load("http://"+ document.domain + "/myfunction/"+text);

Is there any way to do it?

If i call the function by URL directly, not with jQuery, it works well.

Thanks.

0

2 Answers 2

4

You should encode "text" with encodeURI:

var text = encodeURI('hello world this is an example');

This will ensure that your whitespaces are replaced with url compatible characters, which your browser does internally when you're directly accessing the url.

Sign up to request clarification or add additional context in comments.

Comments

1

Calling the function by URL directly might works well in your browser. But it is not future proof. You must encode your url with encodeURI function. After appending user given data.

var text ="hello world this is an example",
url = encodeURI("http://"+ document.domain + "/myfunction/"+ text);
$("#selector").load(url);

And on server side you can do something like this to get back user entered data.

$data = urldecode($_SERVER['REQUEST_URI']);
// This will return
// /myfunction/hello world this is an example

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.