我正在使用 $.post(url, cb)
进行 AJAX 调用。我传入的 URL 可能包含奇怪的字符,例如空格、&
、?
等。
我必须使用$.post(encodeURIComponent(url), cb)
吗?
url
类似于 /foo/weird-char§
。
最佳答案
Do I have to use $.post(encodeURIComponent(url), cb)?
您将必须使用encodeURIComponent()
,但不能在整个URI上使用,仅在数据部分使用(奇怪
和 chars
在您的示例中)。 URL 和 ? &
分隔的参数必须保持完整。如果对整个 URI 进行编码,它将变得不可用。
如果您使用 data
参数将数据添加为 POST 数据:
url = "/foo/possible";
$.post(url, { "weird": "f2(90§§$", "chars": "ß1028490" });
jQuery 的 Ajax 函数会自动处理数据的 URL 编码。
关于url - jQuery $.post - 我必须对 URL 参数进行编码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5244090/