Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URIError: malformed URI sequence?

The below code error's out with URIError: malformed URI sequence? when there is a % sign like 60% - Completed in the URL string from where I need to extract the parameter value e.g. http://some-external-server.com/info?progress=60%%20-%20Completed

   <SCRIPT type="text/javascript">
            function getParameterByName(name) {
                name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
                return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
            }
    </SCRIPT>

I dont have control of the server and need to process the output in my html page.

like image 730
Stacked Avatar asked Dec 20 '13 09:12

Stacked


People also ask

How do you fix a URI malformed error?

How to fix the error. In order to fix "Uncaught URIError: URI malformed" errors in your code, you need to ensure that you are passing valid characters to both encodeURI and decodeURI too. If you would like to convert Unicode code points to UTF8, you can use this online tool.

What does URI malformed mean?

The JavaScript exception "malformed URI sequence" occurs when URI encoding or decoding wasn't successful.

What is a URI error?

The URIError object represents an error when a global URI handling function was used in a wrong way. URIError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .

What is decodeURIComponent?

The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.


1 Answers

I think you need to URI encode the percentage sign as '%25'

http://some-external-server.com/info?progress=60%25%20-%20Completed 

[EDIT]

I guess you could do something like this:

var str = "60%%20-%20completed";
var uri_encoded = str.replace(/%([^\d].)/, "%25$1");
console.log(str); // "60%25%20-%20completed"
var decoded = decodeURIComponent(uri_encoded);
console.log(decoded); // "60% - completed"
like image 65
Slicedpan Avatar answered Sep 28 '22 01:09

Slicedpan