[英]Jquery / PHP - ajax post and encoding
我正在嘗試使用ajax發布發送url字符串,但不知道為什么該字符串會自動編碼(%25)或省略
url = http://www.domain.com.br/berco-elegance-wave-verde-ayoba%2521-6343.html
php接收: http : //www.domain.com.br/berco-elegance-wave-verde-ayoba%21-6343.html
我使用Firebug檢查發布數據,並且url字符串已更改:POST數據url: http : //www.domain.com.br/berco-elegance-wave-verde-ayoba%21-6343.html
我的jQuery代碼:
function save_competitor() {
$.ajax({
type: "post",
url: "<?php echo base_url();?>admin/storeproduct/insert_competitor",
data: "url="+$("#url"),
dataType:"json",
contentType : "application/x-www-form-urlencoded; charset=UTF-8",
success: function (data) {
if(data.success){
alert('ok');
}else{
alert('error');
}
},
error: function (request, status, error) {
if(status=='parsererror')
window.location.href = 'auth/login/';
}
});
}
HTML:
<input type="text" id="url" name="url" value="http://www.domain.com.br/berco-elegance-wave-verde-ayoba%2521-6343.html"/>
是不是$(“#url”)返回一個元素?
$("#url").val()
or
$("#url").attr('href')
您可以從已編碼的html文本中獲取data
。 您能告訴我們$(“#url”)html代碼的一部分是什么樣嗎?
編輯:
功能說明
function urldecode(str) {
return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}
用法:
data: "url="+urldecode($("#url").text());
URL Endoding參考:
space %20
! %21
" %22
# %23
$ %24
% %25
& %26
' %27
( %28
) %29
* %2A
+ %2B
, %2C
- %2D
. %2E
/ %2F
.....
encodeURIComponent和encodeURIComponent將解決此問題
encodeURIComponent(url);
decodeURIComponent(url)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.