简体   繁体   中英

Jquery / PHP - ajax post and encoding

I m trying to send a url string using ajax post but dont not know why the string is auto encoding (%25) or omitting

url = http://www.domain.com.br/berco-elegance-wave-verde-ayoba%2521-6343.html

php receive: http://www.domain.com.br/berco-elegance-wave-verde-ayoba%21-6343.html

I check with Firebug the post data and the url string is already changed: POST DATA url: http://www.domain.com.br/berco-elegance-wave-verde-ayoba%21-6343.html

My jquery code:

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"/>

isnt $("#url") returning an Element?

$("#url").val()
or
$("#url").attr('href')

You get your data from html text already encoded. Can you show us what the part of your $("#url") html code look like ?

EDIT :

Function :

function urldecode(str) {
    return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}

Usage :

data: "url="+urldecode($("#url").text());

URL Endoding References:

space   %20
    !   %21
    "   %22
    #   %23
    $    %24
    %   %25
    &   %26
    '   %27
    (   %28
    )   %29
    *   %2A
    +   %2B
    ,   %2C
    -   %2D
    .   %2E
    /   %2F
   .....

encodeURIComponent and decodeURIComponent will resolve this issue

encodeURIComponent(url);
decodeURIComponent(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM