%cd在ajax中转换为Í到php调用

I have this weird issue which i'm absolutely don't even know how to address...

i have this little AJAX call:

    $('#toexcel').live("click",function() {
        var sql = grid.data().sql;
        $.ajax({
            url: "toExcel.php",
            data: "sql="+sql,
            success: function(response){
                window.location.href = response.url;
            }
        });
        alert(sql);
    });
}); 

which passes sql query from grid object to toExcel. toExcel uses PHPExcel object to output excel file.

now:

when alert pops up it has normally looking query ...LIKE '%cdviled%'...

but when i access this query in toExcel.php it looks like ...LIKE 'Íviled04%'.... and query obviously fails.

why????? how??

%cd is a valid URL-encoded representation of Í and PHP handles it as such.

The solution is simple. Change:

data: "sql="+sql,

to:

data: "sql="+encodeURIComponent(sql),

PHP is interpreting %cd as a URL-encoded entity. You will want to run your parameters through encodeURIComponent() before sending off the ajax request.

Although, you might get better (and more secure) results by simply sending the search term via ajax and letting PHP assemble the SQL on the server side!