此url是有有&符號的,用常規ajax post方式傳到php後會把&符號前後分成一個個單獨的參數...
//如:url=http://localhost/app/index.php?c=show&a=index&from=weixin
這個是一個參數,key是url,其它都是value(不是3個參數),請問如何才能將這個url完整地傳到php呢?聽說可以在js加密這個url,然後在php解密,但是我找了一下,js加密都要引用不少東西,請問有沒有個簡單的做法呢?
我試了使用轉義符,上面的改成
url=http://localhost/app/index.php?c=show\\&a=index\\&from=weixin
php則會報錯說找不到這個控制器...
回複內容:
此url是有有&符號的,用常規ajax post方式傳到php後會把&符號前後分成一個個單獨的參數...
//如:url=http://localhost/app/index.php?c=show&a=index&from=weixin
這個是一個參數,key是url,其它都是value(不是3個參數),請問如何才能將這個url完整地傳到php呢?聽說可以在js加密這個url,然後在php解密,但是我找了一下,js加密都要引用不少東西,請問有沒有個簡單的做法呢?
我試了使用轉義符,上面的改成
url=http://localhost/app/index.php?c=show\\&a=index\\&from=weixin
php則會報錯說找不到這個控制器...
...少年,老師沒教過你 querystring 的參數一定要做 urlencode 的麼=。=
"url="+ encodeURIComponent("http://localhost/app/index.php?c=show&a=index&from=weixin");
PHP 後端按道理應該 $_GET['url'] 是可以直接拿到正常的字串的,如果沒有的話請自行 urldecode 一次。
URL Encode一下參數再傳就可以了
url=encodeURIComponent("http://localhost/app/index.php?c=show&a=index&from=weixin")
參考:http://stackoverflow.com/questions/332872/encode-url-in-javascript
額,剛剛看了一下url編碼,使用這樣的就可以解決問題了
str.replace(/(&+)/g,'%26')
就行了
然後順便看到js有把字串轉換為url編碼的方法encodeURIComponent(),用這個也應該可以的