简体   繁体   English

Rails接收参数作为字符串

[英]Rails receive params as string

I'm building an API with Ruby on Rails. 我正在使用Ruby on Rails构建API。 Because of that, I can't force the params to follow sone restriction. 因此,我不能强迫参数遵循严格的限制。

Currently, I'm having an error with params that contained a Windows newline, because the newline isn't escaped. 当前,包含Windows换行符的参数出现错误,因为没有换行符。

The request body looks like below: 请求正文如下所示:

{
    "name": "Jon Do",
    "address": "6th Street\r\nDistrict City\r\nNation"
}

And it generated JSON::ParserError: 784: unexpected token 并生成JSON::ParserError: 784: unexpected token

How to correctly process that request so params[:address] can be called without error? 如何正确处理该请求,以便可以无错误地调用params[:address]

When passing the data from front-end, make it JSON.stringify , and parse from back-end. 从前端传递数据时,使其为JSON.stringify ,并从后端进行解析。

#Front-end
   address: JSON.stringify(objects)

#Back-end
   JSON.parse(params[:address])

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

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