maram
maram

Reputation: 23

Avoiding transfer ”&” to ”&amp”

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);
});

Upvotes: 0

Views: 2390

Answers (2)

skawt
skawt

Reputation: 49

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;

Upvotes: 2

Eisson Alipio
Eisson Alipio

Reputation: 119

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.

Upvotes: 1

Related Questions