如何在 post 中将内容类型转义为 application/x-www-form-urlencoded

How to escape + in post call content type as application/x-www-form-urlencoded

我有一个内容类型为 ajax 的电话: xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

现在我有这样的数据: "firstName=bob+builder";

当数据发送到服务器时,浏览器将 + 号替换为 space 并且数据变为 firstName:bob builder

有没有办法避开那个 + 号?据我所知,除了应该在服务器端处理外,别无他法,将 space 替换为 + 号。我错了吗?

您应该可以在客户端进行编码。随着 Javascript:

encodeURIComponent("bob+builder"); //bob%2Bbuilder

然后服务器端代码应该简单地解码。