jQuery的$ .ajax URL编码问题

sch*_*one 17 ajax firefox jquery webkit url-encoding

我正在使用jQuery的$ .ajax方法向REST服务发送和检索数据.我提供给$ .ajax方法的一些URL需要空格和其他特殊字符进行编码.

问题在于Chrome,Safari(Webkit)和Internet Explorer浏览器.Firefox POST是一个URL,它被编码但其他浏览器POST到一个未编码的URL.

举个例子:

$.ajax ({
  url: "http://localhost:8080/rest/123/Product Line A/[Product Type B]",
  type: "POST",
  dataType: "json",
  data: { ... },
  success: function(...){},
  error: function(...){}
})
Run Code Online (Sandbox Code Playgroud)

Firefox以下列格式发布URL:

http://localhost:8080/rest/123/Product%20Line%20A/%5BProduct%20Type%20B%5D
Run Code Online (Sandbox Code Playgroud)

Chrome,Safari和IE按以下格式发送网址:

http://localhost:8080/rest/123/Product Line A/[Product Type B]
Run Code Online (Sandbox Code Playgroud)

REST服务接受编码(Firefox)格式 - 有没有一种方法可以使所有浏览器保持一致?

提前致谢!

Aat*_*tch 24

您可以使用javascript的encodeURI()函数将URL编码为"Firefox格式".


Pek*_*ica 8

[Product Type B]以未编码的形式传递是无效的,因此浏览器对其进行的操作是未定义的.

encodeURIComponent()在产品类型部分做一个.