Gissipi_453 Gissipi_453 - 1 month ago 5
Ajax Question

Trying to send special characters via JQuery Ajax to PHP script

I'm sending special characters to a PHP file via JQuery Ajax.

send_to_process.js

var special_charac = '!@#$%^&*()_+-=';
var dataString = 'data=' + special_charac;

$.ajax({
type: "POST",
url: "./process.php",
data: dataString,
cache: false,

success: function (result) {

}
});


process.php

<?php

$data= $_POST['data'];
echo $data;

?>


In the PHP file I'm getting all values except + and &

Why is it so ?

Does JQuery Ajax has got some limitations as to what data can you send to PHP script ?

Answer

These are not AJAX limitations. These are URL limitations. eg & is used to split parameters. Just send data as json object

not:

data: dataString,

but

data: {data: special_charac}

or use encodeURI function to escape data

var dataString = 'data=' + encodeURI(special_charac);
Comments