Javascript not loading due to net::ERR_CONTENT_LEN

2020-02-26 14:32发布

问题:

I've got an app that runs fine locally, but barfs in production. (Classic problem, right?)

In production, the JS isn't firing correctly. When I open up the browser console, I see this error:

net::ERR_CONTENT_LENGTH_MISMATCH

When I look in the network tab of Developer Tools, is shows that it failed on a GET request for text/html.

It's a cryptic error. I've only found two other SO posts that even mention is and they're unsolved. (For the interested: first post and second post)

Any idea (1) what it means or (2) how to resolve it?

回答1:

I am getting Error: net::ERR_CONTENT_LENGTH_MISMATCH

Have a look at your server logs to determine what the real issue is.

For me the problem lay somewhere between nginx and file permissions:

  • tail -f /usr/local/var/log/nginx/error.log or run nginx -t to determine your conf location, where you could specify a custom log path.
  • refresh the asset in your browser, eg http://localhost:3000/assets/jquery/jquery.js

You may see something like this in the logs:

"/usr/local/var/run/nginx/proxy_temp/9/04/0000000049" failed (13: Permission denied) while reading upstream for file xyz

Heres how I fixed:

sudo nginx -s stop
sudo rm -rf /usr/local/var/run/nginx/*
sudo nginx


回答2:

According to this bug report, that error occurs when an external resource length does not match the Content-Length header given in the response.

This might occur because of a misconfigured server, or (as a stretch) maybe some BOM characters got put into the file, or maybe even different lines endings (are you writing on a DOS machine and deploying to a UNIX machine?) may cause issues.

Hope this helps.



回答3:

I had a similar issue when trying to interpret JSON results. It turned out that somewhere along the line an odd character landed in the database - in this instance the culprit was "â??". It is not clear how this value arrived in the database, but it is likely related to HTML encoding issues - "Â" character showing up instead of " " Either way, after removing the odd characters, the problem was solved.



回答4:

I had similar issue [crit] 6889#0: *14817 open() "/var/cache/nginx/proxy_temp/3/02/0000000023" failed (13: Permission denied) while reading upstream

it was because Nginx worker process were not able to access folder /var/cache/nginx/proxy_temp/ - I just changed the folder permissions and everything started working



回答5:

I had similar issue developing locally on MAMP and using Chrome as browser. Same problematic websites on the live servers had no such issues. First I thought it was MAMP that was playing around and I checked settings like PHP versions mismatch, apache version etc., reinstalled, but the issue remained. At the end I just switched to using Brave browser (was delaying the shift to Brave for a while anyway) and that fixed it for me.

Hope this helps.