简体   繁体   English

ajax POST中的键和值都被转义

[英]keys and values from an ajax POST are all escaped

I am trying to access some params from an ajax post in Rails. 我正在尝试从Rails中的ajax帖子访问一些参数。 But my params are all escaped. 但是我的所有参数都逃脱了。 I am not quite whether this is the best way to go about it. 我不确定这是否是最好的解决方法。 If I inspect the params[:event] vairable in Rails I end up receiving it looks something like this 如果我在Rails中检查params[:event]变量,我最终会收到这样的内容

{\"title\":\"None\",\"startdate\":\"2014-01-23\",\"enddate\":\"None\",\"description\":\" Description\"}

I am sure I can use this data if I really wanted to. 如果我真的想的话,我相信我可以使用这些数据。 But is this really the best way to receive data on my backend? 但这真的是在后端接收数据的最佳方法吗?

For reference, ajax 供参考,ajax

$.ajax ({
    type: 'POST',
    url: '/api/calendar',
    dataType: 'json',
    data: {'event': JSON.stringify(this)},

    success: function(response) {
        console.log('Success ', response);
    }

});

Typical data 典型数据

event:{
    "title":"None",
    "startdate":"2014-01-23",
    "enddate":"None",
    "description":"Description"
}

Two questions: 两个问题:

  1. Is this the only way rails would accept data? 这是Rails接受数据的唯一方法吗?

  2. If this is the safest way to post data to my backend what would be the easiest way of accessing the data? 如果这是将数据发布到后端的最安全方法,那么访问数据的最简单方法是什么?

Any help would be appreciated, thank you 任何帮助,将不胜感激,谢谢

The JSON.stringify was the culprit. 罪魁祸首是JSON.stringify。 I was using a backbone model and switched to .toJSON instead. 我使用的是骨干模型,而是改用.toJSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM