User12341234
User12341234

Reputation: 79

Is there a way to shorten URL using Javascript function

I have a Javascript function, that calls window.location.href

Below is the sample example of code.

function xyz () {
    var URL = "https://www.web.com/getNode.jsp?param1=abc|def|ghi|.......";
    window.location.href = URL;
}

Here param1 can accept any nunmber of values. But the problem is if the size of "URL" var is more than 3000 characters, encountering an exception like

500 Internal Server Error.

But my requirement is to pass all the param1 values to the JSP no matter how long param1 is.

Is there any way to solve this Problem?

Can any one please help.

Thanks.

Upvotes: 2

Views: 513

Answers (2)

fdreger
fdreger

Reputation: 12495

Let's summarize the facts:

  • You have a requirement to send URLs of unlimited length (quotes: "no matter how long param1 is" and "I have no option to send data using POST")
  • Your server is configured to reject URLs longer than 3000.

The only logical conclusion is: it will not work, no matter how you try. Unless you break (or loosen) the rules.

Any solutions that you may think of will be workarounds which will NOT solve the problem in general, but might be "good enough". You are the only person who can know what "good enough" is - in your context.

Example solutions:

  • reconfigure the server to accept longer URLs
  • pass the actual data in a cookie and only put part of it in the url
  • use a websocket connection or an additional XHR call to send the data ahead of the redirect

Note that all those "solutions" require cheating, as your stated goal remains impossible.

BTW: url shorteners are NOT a solution - they don't do magic, they just redirect people from a short url to a long one. If the target server rejects the longer url, it will still not work.

Upvotes: 4

Vadim Hulevich
Vadim Hulevich

Reputation: 1833

you can try change method from GET to POST and add all your params data inside body. Body in POST can contains much more

Upvotes: 0

Related Questions