Remove escape character from Jquery Post

蓝咒 提交于 2019-12-13 05:04:50

问题


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 :)


回答1:


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',
    ),
  ),
)



回答2:


You're calling $.post incorrectly. The second argument is all the POST parameters, it's not an options structure. If you want to pass options, you have to use $.ajax:

$.ajax("updateremission.php", {
    data: { rem: jsonRemission },
    dataType: "json",
    success: function(data, status) {
        if(status=="success"){
            alert("Good Message");
        }else{
            alert("Bad Message");
        }
    }
});

You shouldn't use processData: false, because that will prevent the parameter from being put into $_POST['rem'].



来源:https://stackoverflow.com/questions/24659708/remove-escape-character-from-jquery-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!