Random 404 from localhost - javascript

I'm running a local server in python
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
EDIT: This is the readable version of the command string passed to python -c, so in pseudocode the code block above could be resumed like this: python -c $'command' "$port" where
the command part is:
import SimpleHTTPServer
map = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map
map[""] = "text/plain"
for key, value in map.items():
map[key] = value + ";charset=UTF-8"
SimpleHTTPServer.test()
and I get (seemingly) random 404 errors that prevents my js to execute correctly.
More specifically I am serving a static html page and its linked javascript files.
Chrome tells me net::ERR_ADDRESS_UNREACHABLE and sometimes net::ERR_CONNECTION_REFUSED on this script tag on the index.html page.
<script type="text/javascript" src="http://localhost:8000/lib/fonts.js"></script>
I have 3 weird behavior I don't understand:
if I inspect the terminal log where the python server is running I don't have any 404 error. In fact it says that the file was correctly served (200).
if I go to chrome's Network panel it tells me: Failed to load response data but if I 'open link on a new tab' I can actually see the file!
If I load the file http://localhost:8000/lib/fonts.js on another tab and I refresh many times I never get errors.
sometimes I don't get any 404 errors but my js does not work at all (no js errors)
So who is causing the problem? Chrome, Python or some async behavior in my js?
I suspect that the js files are not loaded in order so other js scripts could not find and use them..
EDIT2:
If I switch server, for example using the built-in php server:
php -S localhost:8000
I won't get the errors

Ok this seems was caused by two different issues:
I've got rid of any async attributes on the <script> nodes. Especially it seems that loading jquery from google with the async attributes was causing most of the problems. Most probably the order of the scripts was not preserved but I've never got any error about jquery being undefined, I was getting an error on the missing script /lib/fonts.js weird..
Even tough removing async made things more reliable, more or less once every 10 page refresh I still got errors of the type net::ERR_ADDRESS_UNREACHABLE or net::ERR_CONNECTION_REFUSED on different external resources, sometimes on a js file, sometimes on a css file. Changing the python server with the php built-in server seems removed the remaining random errors.
So php -S localhost:8000 is more reliable (in my case) than the python counterpart.
NOTE: Even tough I've solved the problem I still don't know exactly what was causing the errors so if someone else has a better answer than this he's very welcome to post. I will leave the answer unaccepted for a while.

Related

Occasionally getting Reference Error in Javascript console

I am occasionally getting different types of Reference errors logged in chrome console.
For eg: require not defined
Cookies not defined
lozad not defined
The code is bundled in static-internal-bundle.js which is a combination of 7 different util files and minified using gulp-minify.
This error is coming mainly when the url is hit from paid.braintree.com Ad urls and has utm_source, utm_medium and utm_campaign as query parameters.
This error is not consistent and mostly does not reoccur if the site is refreshed.
Also, it has been logged in sentry for all types of devices and Os for over 1.5k times in 6 months. Though it gets logged in console, nothing seems to break on the webpage.
What could be the possible reasons for this type of error?
<script type="text/javascript" src="https://static.pens.com/576801d7b3a368c06ba1944c07fe260b970da596/build/static-internal-bundle.js"></script>
This is the script that gets added into the page in which the error is coming.
I would add this as a comment but StackO won't let me without 50+ rep... I'll edit as needed.
Possibly the error could be in using "require" on the browser side, as browsers do not implement require
This sounds like a problem I have had with Babel, if it is being used I would try updating.

Failed to load javascript script in Elasticsearch

I wrote a javascript script for script_score in function_score, but I get the following error on elasticsearch service start. I am running Ubuntu 14.04, elasticsearch 1.4.5. I sadly cannot post the code here.
What would constitute a valid return value? Is there any way to debug the score script or print to elasticsearch logs?
[2015-05-23 21:00:18,093][INFO ][script ] [Soulfire] compiling script file [/etc/elasticsearch/scripts/****.js]
[2015-05-23 21:00:18,180][WARN ][script ] [Soulfire] failed to load/compile script [****]
org.mozilla.javascript.EvaluatorException: invalid return (Script1.js#14)
Thanks
Update:
I tried a script with just return 0.5;. I still get the error:
[2015-05-23 22:52:48,527][INFO ][script ] [Warrior Woman] compiling script file [/etc/elasticsearch/scripts/test.js]
[2015-05-23 22:52:48,609][WARN ][script ] [Warrior Woman] failed to load/compile script [test]
org.mozilla.javascript.EvaluatorException: invalid return (Script1.js#1)
Update 2:
I removed the return keyword from my script, e.g:
return 0; becomes 0;
This results in the errors subsiding, but I continue to get unexpected results. I would still like to know how I can log from within the js script.
From the quick look on the log,ES is trying to say it is unable to load or compile your Javascript script.Also please add more info, if this is your dynamic script or standalone script stored in script files on host.
If you have't installed javascript-lang plugin for ES, there is no way ES can compile and run your Javascript.You must take a look at the following link
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
Even groovy is now no more sandboxed for dynamic scripting because of security concerns with ES 1.5.0 + as ES people have comeup with their own security module.
revert back if my answer gave you any help.
Thanks

JSONP and DOCTYPE Errors

I'm running into a weird problem.
I'm communicating with my server using AJAX. (I'm running my web application on localhost).
Server is located on, say, http://www.example.com
To bypass the Same Origin Policy, I'm using JSONP. I dynamically create a <script> tag and load the data from my server.
So far so good.
Then I decided to upload my web application to this subdomain: http://m.example.com
That's when I run into crazy errors. Sometimes the page loads, sometimes it doesn't. When it doesn't load, Firebug throws a DOCTYPE error.
I did some research and came across this stackoverflow post: firebug returns syntax error in doctype?
Quoting an answer in this link:
This usually happens because you are loading an HTML document as a script. This is often caused by <script src=""></script> (i.e. a relative URI pointing at the current, HTML, document)) or one of the scripts pointing to a 404 error.
Pretty helpful stuff. Based on all that, I've concluded from all the above that whenever my server responds slowly, the <script> tag's src attribute is null. Since that throws a 404 error, I get a DOCTYPE error in Firebug. Whenever my server responds quickly, there are no issues and everything works fine.
How do I solve this problem? I could put a manual timeout or something, but that wouldn't exactly be foolproof and an elegant solution.
Any help guys?
EDIT:
Here's some code:
This function is used to create the script tag dynamically:
function appendScriptToHead() {
var element = document.createElement("script");
element.src = 'http://www.example.com/?data&callback=callfunction';
document.getElementsByTagName("head")[0].appendChild(element)
}
This callback function is called when the above url containing JSONP data is loaded:
function callfunction(response) {
alert(response);
}
I think there's a bit of misunderstanding here. Your script element will always have its src property set, but its contents depends on your server's response. I doubt it'll be error 404 (as it refers to the element not found, which is hardly repetitive), but it can be of 500 flavors.
I suggest debugging your queries just as they are (i.e., opening http://www.example.com/?data&callback=%callfunction% with your browser or some scripted HTTP UserAgent, if you feel industrious), to see what might be wrong with the logic which selects the script to be loaded.

Apache "undefined" file requested

I am having an interesting error with Apache (I think Apache is the issue). I noticed many of my JavaScript files were not being served. When I went and looked at my Apache logs, I saw a whole slew of:
.... [error] File not found: /webroot/site/undefined .....
I have a strange feeling the issues are related. I am requesting the correct script src paths for sure.
Any reason why the heck "undefined" would be requested?
Additional info: PHP script, clients are embedded IE8 applications.
If you really do call the correct script URIs (“script src paths” as you say) the problem can only lie in your apache configuration.
Check your rewrite rules etc.
You may also want to check your access log, whether strange/wrong paths/URLs were called.

Unusual javascript HTTP caching / undefined issues

Ok. I have never seen anything like this before and I am quite confused... On a website I am working on, there are two JavaScript includes. (Well, many more, but the problem occurs with only two). We'll call these A.js and B.js.
A.js defines a JavaScript singleton A. Likewise B.js defines a JavaScript singleton B. The B class depends on methods from A.
I have almost all my code wrapped up in try { ... } catch { ... } blocks, and on error, the system generates an AJAX request to email me the details of the error.
On one system here in my office (Firefox 3.0 for Mac) I am seeing the strangest behavior. Once in a while, maybe once every hour or two, but with no specific pattern, I get an error email from class B: "ReferenceError: A is undefined."
I have no idea why/how A would be undefined sometimes. When I look in the apache logs, during the times I receive the error, the client is requesting /A.js, before requesting /B.js, and the server is responding with a 304 (Not Modified)
I somehow suspect the issue is that the file has expired from the client cache however the server doesnt send the file thus it is never executed and A is undefined. But that's just a guess and I don't understand why that would happen. And if I am guessing right, how do I fix it???
If it matters, mod_disk_cache was enabled on the server, and I have disabled it to see if that was causing the problem. If so, maybe this belongs on ServerFault...
Any suggestions very welcome!!
If you have the Webdevelopers toolbar installed in Firefox, activate "Disable Cache" it helps a lot on debugging.
start calling the first js function after! the page has loaded, do this by <body onload="startmyfunction()"> or similar.

Categories

Resources