避免将“&”转移到“&amp”

问题描述 投票:-1回答:2

我在将URL中的“&”转移到“&amp”时遇到问题。

[当我的系统从另一个系统请求API时,我的系统将使用新选项卡显示API响应。

响应是一个包含两个用&分隔的值的URL

URL中的&转移到&amp,这导致我的系统出现问题。

附件图像enter image description here中的URL示例

正确的URL应为xxxxxxrid = 1182&eid = 25

后端代码

def url_with_params(reservation_id)

"#{self.url}?rid=#{reservation_id.to_s}&eid=#{self.id.to_s}"

 #URL ex: https://XXXXXXXXXXXX?rid=1184&eid=1. 

end

客户端代码

$(document).on("click", '.dev-submit-instructions-agreement', function(){
    window.open("<%= @exam.url_with_params(@reservation.id) %>", 'newwindow', 'width='+screen.width+', height='+screen.height);
});
javascript url encoding amp-html
2个回答
1
投票

您必须使用encodeURIComponentdecodeURIComponent方法:

const  encodedURL = encodeURIComponent('https://example.net?id=1182&eid=25');
// "https%3A%2F%2Fexample.net%3Fid%3D1182%26eid%3D25"

恢复初始URL:

const url = decodeURIComponent(encodedURL); 
// "https://example.net?id=1182&eid=25"

希望获得帮助。


0
投票

我会尝试在服务器端进行HTML解码,但我假设这是数据来自第三方服务器。使用JavaScript,在客户端,您总是可以尝试如下操作:

x='some data';
x=x.replace(/&amp;/g,"&");
window.location=x;
© www.soinside.com 2019 - 2024. All rights reserved.