0

I have a problem with transfer ”&” to ”&amp” in the URL.

When my system request an API from another system then my system display the API responses with the new tab.

The response is a URL that contains two value separated by &

The & in the URL is transfers to &amp which it causes an issue to my system.

URL example in the attached image enter image description here

The right URL shall be xxxxxxrid=1182&eid=25

backend code

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

client-side code

$(document).on("click", '.dev-submit-instructions-agreement', function(){
    window.open("<%= @exam.url_with_params(@reservation.id) %>", 'newwindow', 'width='+screen.width+', height='+screen.height);
});
maram
  • 23
  • 5
  • URLs sent over the Internet using the ASCII character-set. Check this for decoding options: https://stackoverflow.com/questions/4292914/javascript-url-decode-function – A. Meshu Apr 01 '20 at 19:25

2 Answers2

1

You have to use encodeURIComponent and decodeURIComponent methods:

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

to recover the initial URL:

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

hope this help.

1

I would try HTML decoding on the server side, but I'm assuming that this is data is coming from a third party server. With JavaScript, on the client side, you could always try something like this:

x='some data';
x=x.replace(/&amp;/g,"&");
window.location=x;
skawt
  • 39
  • 4
  • "data is coming from a third party server, if you mean the URL yes, its coming from a third party. can the party do something from their side to avoid this? as I know we have done like this things but it still convert to "&" – maram Apr 02 '20 at 16:49