doujing5150 2013-03-12 17:38
浏览 69
已采纳

jQuery.post正在发送转义字符

I have a string created by recipeBlob = JSON.stringify(recipeData). It is a long string but here is a sample of it:

"properties":{"Energy":{"val":90.6,"unit":"cal","rdi":"2000"},"Protein":{"val":0.4,"unit":"g","rdi":"50.00000000"},"Fat":{"val":0.6,"unit":"g","rdi":"65.000000000"},"Carbohydrates":{"val":23.3,"unit":"g","rdi":"300.0000000"},"Dietary Fiber":{"val":4.1,"unit":"g","rdi":"25"},"Sugars":{"val":18.8,"unit":"g","rdi":null}

which I send via $.post like this:

$.post('scripts/save_blob.php', {
            blob : recipeBlob
        });

The $string = $_POST['blob'] that arrives at my PHP script is showing up like this:

{\"properties\":{\"Energy\":{\"val\":90.6,\"unit\":\"cal\",\"rdi\":\"2000\"},\"Protein\":{\"val\":0.4,\"unit\":\"g\",\"rdi\":\"50.00000000\"},\"Fat\":{\"val\":0.6,\"unit\":\"g\",\"rdi\":\"65.000000000\"},\"Carbohydrates\":{\"val\":23.3,\"unit\":\"g\",\"rdi\":\"300.0000000\"}

When I run json_decode($string) it is returning error #4 which is json syntax error.

I don't know at which part of the process the characters are being escaped, which I need to avoid.

  • 写回答

1条回答 默认 最新

  • doulu1867 2013-03-12 18:30
    关注

    @Juhana provided the most direct answer to the question, PHP magic quotes were turned on on the server, thus automatically escaping quotes.

    @dfsq provided the solution I actually used, which is to just send the javascript object without stringifying it. it arrives to the PHP script as an array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能