jQuery $ .post - 我必须对URL参数进行编码吗?

nor*_*gon 11 url jquery

我正在与AJAX通话$.post(url, cb).我传递中的URL可能有奇怪的字符,如空间,&,?等等.

我必须使用$.post(encodeURIComponent(url), cb)吗?

url是这样的/foo/weird-char§.

Pek*_*ica 13

我是否必须使用$ .post(encodeURIComponent(url),cb)?

不得不使用encodeURIComponent(),但没有对整个URI,仅在数据部分(weird以及chars在你的例子).URL和? &分隔参数必须保持不变.如果您对整个URI进行编码,它将变得无法使用.

如果要使用data参数将数据添加为POST数据:

url = "/foo/possible";
$.post(url, { "weird": "f2(90§§$", "chars": "ß1028490" });
Run Code Online (Sandbox Code Playgroud)

jQuery的Ajax函数会自动处理URL编码.