0

我正在尝试通过参数(带有空格)将文本传递给加载函数,但它似乎不起作用。

目前我正在这样做:

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

有什么办法吗?

如果我直接通过 URL 调用该函数,而不是使用 jQuery,它运行良好。

谢谢。

4

2 回答 2

4

您应该使用 encodeURI 对“文本”进行编码:

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

这将确保您的空格被替换为与 url 兼容的字符,当您直接访问 url 时,您的浏览器会在内部执行此操作。

于 2012-10-09T14:49:53.727 回答
1

直接通过 URL 调用函数可能在您的浏览器中运行良好。但这不是未来的证据。您必须使用encodeURI函数对您的网址进行编码。附加用户给定数据后。

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

在服务器端,您可以执行类似的操作来取回用户输入的数据。

$data = urldecode($_SERVER['REQUEST_URI']);
// This will return
// /myfunction/hello world this is an example
于 2012-10-09T15:14:08.567 回答