我正在尝试通过参数(带有空格)将文本传递给加载函数,但它似乎不起作用。
目前我正在这样做:
var text ="hello world this is an example";
$("#selector").load("http://"+ document.domain + "/myfunction/"+text);
有什么办法吗?
如果我直接通过 URL 调用该函数,而不是使用 jQuery,它运行良好。
谢谢。
我正在尝试通过参数(带有空格)将文本传递给加载函数,但它似乎不起作用。
目前我正在这样做:
var text ="hello world this is an example";
$("#selector").load("http://"+ document.domain + "/myfunction/"+text);
有什么办法吗?
如果我直接通过 URL 调用该函数,而不是使用 jQuery,它运行良好。
谢谢。
您应该使用 encodeURI 对“文本”进行编码:
var text = encodeURI('hello world this is an example');
这将确保您的空格被替换为与 url 兼容的字符,当您直接访问 url 时,您的浏览器会在内部执行此操作。
直接通过 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