Sunday, 30 July 2017

php - Remove escape character from Jquery Post




To my issue there are several similar questions but I haven't found a good one that would help me solve my problem. My problem is:



I want to convert a JQuery object into a Json String, and then post this string to a PHP webPage, This is running very good. But when I received it on the server(php page) it is full of escape characters.



Here is my code on the client:



var jsonRemision =  JSON.stringify(remision,false); 

$.post("updateremision.php",

{
rem:jsonRemision,
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: false,
},

function(data,status){
if(status=="success"){
alert("Good Message");

}else{
alert("Bad Message");
}
});


and here is the code on the server:



$remision = json_decode($_POST['rem']);



Now, when I see whats inside of $_POST['rem'] is full of escape characters \" . These escape character are not allowing me to jsondecode... The json full of escape characters looks like this:



{\"id\":\"12\",\"fecha\":\"2014-06-25\",\"ciudad\":\"Manizales\",\"camion\":\"NAQ376\",\"driver\":\"16075519\",\"cant\":\"0\",\"anticipos\":[{\"type\":\"1\",\"com\":\"Comment\",\"costo\":\"1234\"}]}


How can I remove the escape characters ?? thanks in advance for any comment or help :)


Answer



I actually just recently had the same issue.




I fixed it by using stripslashes();



This should work ok unless you actually do have slashes in the data.



var_export(json_decode(stripslashes('{\"id\":\"12\",\"fecha\":\"2014-06-25\",\"ciudad\":\"Manizales\",\"camion\":\"NAQ376\",\"driver\":\"16075519\",\"cant\":\"0\",\"anticipos\":[{\"type\":\"1\",\"com\":\"Comment\",\"costo\":\"1234\"}]}'), true));


outputs:



array (

'id' => '12',
'fecha' => '2014-06-25',
'ciudad' => 'Manizales',
'camion' => 'NAQ376',
'driver' => '16075519',
'cant' => '0',
'anticipos' =>
array (
0 =>
array (

'type' => '1',
'com' => 'Comment',
'costo' => '1234',
),
),
)

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...