我正在使用jQuery的$.ajax方法向REST服务发送和检索数据。我提供给$.ajax方法的一些URL需要对空格和其他特殊字符进行编码。
问题在于Chrome、Safari(Webkit)和Internet Explorer浏览器。Firefox会将已经编码的URL进行POST,而其他浏览器则会将未编码的URL进行POST。
例如:
问题在于Chrome、Safari(Webkit)和Internet Explorer浏览器。Firefox会将已经编码的URL进行POST,而其他浏览器则会将未编码的URL进行POST。
例如:
$.ajax ({
url: "http://localhost:8080/rest/123/Product Line A/[Product Type B]",
type: "POST",
dataType: "json",
data: { ... },
success: function(...){},
error: function(...){}
})
Firefox以以下格式POST URL:
http://localhost:8080/rest/123/Product%20Line%20A/%5BProduct%20Type%20B%5D
Chrome、Safari和IE以以下格式POST URL:
http://localhost:8080/rest/123/Product Line A/[Product Type B]
这个 REST 服务接受编码为 Firefox 格式的内容 - 有没有办法让所有浏览器都统一使用这种格式呢?
谢谢!
encodeURI()
更方便,因为它允许你一次性运行整个 URL。 - PekkaencodeURIComponent()
将/
编码为%2F
,当在 $.ajax url 中使用时会导致错误Uncaught SyntaxError: Unexpected token C
。 - David Torres