JSON数据正在转换为编码的URL

时间:2017-09-05 16:01:59

标签: json rest api

我想直接在url中发送JSON对象。

之类的东西
https://example.com/api/{"node1":[{"id":"32","Title":"000w","type":"Final"}]}

但是当我创建请求时,它会将此网址转换为

https%3A%2F%2Fdomain.com%2Fapi%2F%7B%22node1%22%3A%5B%7B%22id%22%3A%2232%22%2C%22Title%22%3A%22000w%22%2C%22type%22%3A%22Final%22%7D%5D%7D

我不想像我想要以第一种方式点击服务器那样传递。

1 个答案:

答案 0 :(得分:0)

如果你 希望在网址中发送一堆JSON,你应该对其进行字符串化和编码:

var yourObject = {"node1":[{"id":"32","Title":"000w","type":"Final"}]};
var encoded = window.btoa(JSON.stringify(yourObject));

console.log(encoded);

给出:

"eyJub2RlMSI6W3siaWQiOiIzMiIsIlRpdGxlIjoiMDAwdyIsInR5cGUiOiJGaW5hbCJ9XX0="

要让你的对象回到另一端,你可以这样做:

JSON.parse(atob("eyJub2RlMSI6W3siaWQiOiIzMiIsIlRpdGxlIjoiMDAwdyIsInR5cGUiOiJGaW5hbCJ9XX0="))

OR

要解码您在问题中的最终结果,请使用decodeURIComponent

var theString = 'https%3A%2F%2Fexample.com%2Fapi%2F%7B%22node1%22%3A%5B%7B%22id%22%3A%2232%22%2C%22Title%22%3A%22000w%22%2C%22type%22%3A%22Final%22%7D%5D%7D';
decodeURIComponent(theString);

给你回复:

https://example.com/api/{"node1":[{"id":"32","Title":"000w","type":"Final"}]}