Uncaught SyntaxError: Unexpected token <: - javascript

I have an opencart shop. Locally, uploading images work without a glitch. Online, it works only if the image is very small. If I upload an image of, say 300kb, the loading gif next to upload keeps spinning and I get the errors:
ajaxupload.js:
Line 609: if (response) {
Line 610: response = eval("(" + response + ")");
Line 611: } else {
Line 612: response = {};
Line 613: }
Why is this happening?
EDIT:
I did console.log(response) and you were right, what came back was the HTML of the 404 page. But how can it be too big? It works if the image is 100kb but doesn't if it's 130kb.
Why did I get downvoted?

Your Post request is 404ing so the response is not parseable JSON. Looks like your URL has unencoded /s in the query variables. Make sure to use encodeURIComponent() or some other function to make sure your url is properly escaped

From the error I believe that your response is not a valid JSON.
By the way eval is evil so it's much better to use JSON.parse
You can check your JSON here: http://jsonlint.com/
Edit: somebody asked why eval is evil so here are some reasons
it's slower than JSON.parse since it actually starts a compiler
if not done correctly you can end up with XSS attacks
it inherits the execution context and the scope in which its invoked
If you need to support old IE versions you also don't need to use eval(). You could use the excellent JSON library: https://github.com/douglascrockford/JSON-js
This will offer you... JSON.parse support for old IEs.

Try to rewrite the line just before that error.. (don't copy and paste it).. Works sometimes

Related

"Uncaught (in promise) DOMException: Failed to load because no supported source was found" when playing an audio with special characters

I'm playing some audios from a folder in my page, and most of them play without any issues, but when it comes to play a file with a name like "jär" i get this error:
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
I don´t know if it has something to do with my web browser, I run my project on Chrome. I'm using JavaScript and JQuery
The error happens only with files that have some kind of accent or special character on its name (like ä). What I wanna do is, of course, to play such audio regardless its name. Any help is appreciated. Thank you so much.
Most likely what's happening is that your browser isn't resolving URLs with Unicode characters. When pulling your media files, try wrapping it in a encodeURIComponent(). This will format the URI according to the proper conventions, using percent encoding for special characters. For example, jär becomes j%C3%A4r.
x = 'jär'
console.log(x)
console.log(encodeURIComponent(x))
Just wanted to let you know that I finally solved my problem, it seemed I had to encode the value on ISO 8859, so all I had to do was to wrap the value in an escape clause like:
var encode = escape("jär")
And that's it. It worked like a charm. I hope this solution helps to any other who has this issue.

How to decompress gzip xhr response in javascript

I have a gzipped response from a web request that I need to decompress in JavaScript (actually, in the success function of an AJAX call - my code is running in a headless browser, and doesn't have the built-in gzip processing support offered by a full browser). I've been looking around for an answer but I'm a bit stumped.
Ideally, what I'd like to have code for is:
var my_decompressed_string = someGzipDecompressor(xhr.responseText);
I found, what I thought was an answer at JavaScript implementation of Gzip but that may not be the answer I was hoping for. When trying to use the mentioned jsxcompressor library by way of the following code snippet
var my_decompressed_string = JXG.decompress(xhr.responseText);
... I get ...
TypeError: 'undefined' is not an object (evaluating '(new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(str))).unzip()[0][0]')
Looking at that function in more detail, if I break up the code being executed by the decompress() function, I get what I assume is something good returned by the inner part ...
JXG.Util.Base64.decodeAsArray(xhr.responseText)
... but undefined returned for the outer part...
JXG.Util.Unzip( ... )
So, that may be a dead end of course, but if anyone knows a way that my original question should be possible, or has had any better luck with jsxcompressor.js I'd appreciate it.
Of course, I can force my AJAX request to return a 'deflate' response but the size of the page (for which I have no control over) is pretty large and requesting gzip is an attempt at speeding up my page load time.
jsxcompressor.js requires base64 encoded string to decompress, you should use:
var my_decompressed_string = JXG.decompress(btoa(xhr.responseText));
if your headless browser does not support btoa then you should use a base64 encoding library, if your node or iojs there are plenty of base64 npm packages.

IE not loading page with Javascript and Raphael

I'm testing out a website that runs fine on Firefox (Win/Mac), Chrome (Win/Mac) and Safari. I'm having difficulty with Internet Explorer unfortunately. I get the following error message:
SCRIPT65535: Unexpected call to method or property access.
raphael-min.js, line 8 character 64961
I've taken a look at the debug output which looks like just takes me to a part of the Raphel library:
c=a.getScreenCTM()||a.createSVGMatrix()
I've searched for this error message online, but I don't understand what solution is relevant to this case as I've no idea what is causing the problem. I am also using the jQuery library. Are there any tests that I can do that can give me more information about the source of the problem?
I just found how to patch this, in order to keep the compressed version of Raphael.
Replace (don't forget the coma):
c=a.getScreenCTM()||a.createSVGMatrix(),
By that (dont't forget the end space):
c;try{c=a.getScreenCTM()||a.createSVGMatrix()}catch(e){c=a.createSVGMatrix()};var
Works fine ! :)
Means :
c; : declaration of variable c, and stop the first instruction.
try{c=a.getScreenCTM()||a.createSVGMatrix()}catch(e){c=a.createSVGMatrix()}; : our instruction, surrounded by a try/catch, to avoid IE error
var + a space : (don't forget the space!) allow us to continue to declare variable
I found out that it's an issue with compression (of the js file). I had the exact same issue and I had been searching for a solution. Guess what? I tried it with the uncompressed Raphael file and voila! No more issues. Compressed file needs a tweak, it seems.

JSON.stringify and JSON.parse not working in IE9?

I'm using JSON.Stringify and JSON.parse everywhere and it works fine with Firefox. It's working no more with IE9 nor does it work in IE8. What can I do?
JSON.stringify starts with a lower-case s. Both stringify and parse are available in IE8+, but only in standards mode.
Prepend your document with <!DOCTYPE html> if you're currently using quirks mode. Also, watch the capitalization of the JavaScript methods you call - all built-in ones start with a lower-case character.
why do you want to depend on the browser having the object instead just include the script file by Douglas Crockford.. You can find the minifed file here: http://www.json.org/js.html
Once imported you dont have to worry abt the method existing in a browser.
For an alternative, in a scenario where you might need to run in strict mode for whatever reason (I have another library that includes "use strict"), you can look here: https://github.com/douglascrockford/JSON-js. I modified this to check first if JSON is undefined, and only generate the function JSON.parse if it is:
if (typeof JSON === "undefined") {
var JSON = {
parse: <insert value of json_parse from library here>
};
}
My issue was application code not working in IE9 (strict mode being used by a participating library, I believe). That solved the problem for me.
the mere issue is, that sending UTF-8 headers will invalidate the JSON (IE doesn't/didn't like that). as the issue is described, that might still apply for IE9... once wrote a how to, a few years ago. adding JSON support to a browser which can parse native JSON is probably not the optimal solution, since it produces useless overhead - only because failing to deliver the JSON in the expected format.

Catch Malformed XML in Javascript

I'm using prototype to make Ajax requests. Occasionally the responses contain malformed XML. Prototype is using the onException callback, but the first error that is thrown is when I am trying to access a particular node.
I know Firefox recognizes the malformed response, because if I request the xml via the address bar, Firefox presents the error.
Is there a way for me to catch a "malformed xml" error in javascript?
With javascript typically you're relying on the browser to parse the XML for you. If the browser can't parse it due to it being malformed, you're going to have to tackle it manually. There appears to be a library for doing that at http://xmljs.sourceforge.net/ . I haven't used it myself, but it appears solid. Then again, it might also throw malformed xml errors at you.
What's causing the malformed xml? Maybe there's something you can do on that end?
And finally, if you're just trying to access some part of the document's data, you could consider using a regular expression:
doc = "<one><two>three</two></one>";
captures = doc.match(/<two>(.*)<\/two>/); // returns ["<two>three</two>", "three"]
data = captures[1]; // "three"

Categories