当我通过fetch发布json.stringify时,我收到了错误的响应,问题来自于json.stringify正在生成的转义引号。当我手动删除它时,它可以工作,但我需要自动完成。
var order = {
"from_country": "US",
"line_items": [
{
"quantity": 1,
"unit_price": 19.95
}
],
"to_country": "US"
};
var body = JSON.stringify(order);
var body将显示为:
{"from_country":"US","line_items":"[{\"quantity\": 1, \"unit_price\": 19.95}]","to_country":"US"}
我希望它显示为:
{"from_country":"US","line_items":"[{"quantity": 1, "unit_price": 19.95}]","to_country":"US"}
问题是我的文件包含原型库。
我修复了冲突,同时仍然通过添加此代码来维护原型的功能(我认为) -
JSON = JSON || {};
JSON.stringify = function(value) { return Object.toJSON(value); };
JSON.parse = JSON.parse || function(jsonsring) { return jsonsring.evalJSON(true); };
我首先偶然发现这是问题所在:https://stackoverflow.com/a/20331694/8326722引导我到https://stackoverflow.com/a/1327371/8326722,然后我从评论中添加了一些内容以使其与对象一起工作。
如果有人能解释我使用的代码是如何工作的,那就太好了。