2012-10-09 24 views
0

我想通過參數(帶有空格)傳遞一個文本到加載函數,它似乎不起作用。將文本參數傳遞給jQuery上.load函數的url變量

目前,我這樣做:

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

有沒有辦法做到這一點?

如果我通過URL直接調用函數,而不是使用jQuery,它運行良好。

謝謝。

+0

記住urlencoding –

回答

4

你應該編碼「文本」與是encodeURI:

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

這將確保您的空格被替換爲URL兼容字符,您的瀏覽器內確實,當你直接訪問的URL。

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