const List = sku_id skuId amount amount Railsajax url cartadd type POST dataType json data JSONstringifyList提交的数据是 Parameters sku
你可以尝试使用Rails的params方法来获取提交的参数,而不是直接使用JSON字符串。以下是修改后的代码示例:
const List = {
sku_id: skuId,
amount: amount,
}
Rails.ajax({
url: '/cart/add',
type: 'POST',
dataType: "json",
data: List,
})
这样提交的数据将会是 Parameters: {"sku_id"=>"11", "amount"=>"1"}。
在原始的代码中,参数被转换为JSON字符串并作为整个字符串发送给服务器。因此,服务器收到的参数被包装在一个键为{"sku_id":"11","amount":"1"}的哈希中。
原文地址: https://www.cveoy.top/t/topic/ifXP 著作权归作者所有。请勿转载和采集!