http://localhost/undefined 404 (Not Found)

2020-07-03 07:04发布

问题:

I was not clear in my problem description. Let me try again.

NB. This is a problem common to my production, staging, and development sites (not just to my delevlopment site as the commenters seem to have thought).

NB. This problem occurs bowsing with Chrome and not with Firefox.

The problem occurs at the site home page: http://www.example.com (or with "/index.php appended"). The page loads successfully, but the Console pannel of Chrome's Developer Tools shows a 404 error. The message reads "GET http://www.example/undefined 404 (Not Found)". It always shows the error as occuring at "(index):1" which is the DOCTYPE line. Whatever is generating the problem, it is not that.

The page is loading fine, I just get this odd message in the Developers Tools Console. (I probably wasn't clear about that in the original problem description.) Firfox's console shows no such error.

I want to track down what is generating this error. In particular, I would like to find where Chrome is getting the "undefined" url. My guess (but only a guess) is that one of my many Javascript files is genrating a URL from an undefined variable. Chrome's location of the problem, "(index):1", is wrong. I would appreciate any ideas as to how to get a more useful error message out of Chrome: the name of the file where the URL was found would help. If I can do this without having set breakpoints in all my Javascript files one by one, that would be particularly nice. :-)

The original question with URL's removed (I'm limited in the number of links I'm allowed with a reputation < 10) follows. You may not want to read it as people have been confused by it.

We have rather complex Drupal website. When going to the homepage, the Chrome (Version 45.0.2454.85 m) Console shows me an error: "[URL removed] 404 (Not Found) (index):1". (I'm using the development site on my local computer because minification is turned off there.)
Okay, I think, I know what to do about that: Go find the offending spot in whatever file, scratch my head a few times, get a brilliant idea, and fix the problem. One little difficulty: the problem is surely not occurring at index, line 1 (which I assume is what "(index):1" means). The "undefined" in the error message suggests but does not prove that the offender is one of our about a dozen JavaScript files. Presumably some variable somewhere is undefined. So how do I get Chrome to cough up the location where the offense is occurring? It is not giving me any undefined value JavaScript errors, just a 404 on [URL removed]/undefined . Thanks in advance for any ideas.

Edit: I forgot to add that Firefox is showing no such error.

回答1:

I found where this issue could appear from.

  1. Make sure that all the generated CSS (styles) that set a background-image or the like, where there's an url() actually point somewhere; if it doesn't point anywhere the error will set to the .html DOCTYPE.
  2. Check the src of your image tags; make sure that they are not set by JavaScript to undefined.
  3. Also check the src of your script files, even when it'd be much rarer in that case to happen.

So yes it's awful that it actually doesn't point where the actual error is but in fact has to be with a lookup resource that it isn't able to find; we probably don't even want it to search. Eg. when the element is invisible and we don't set any image; but we still pass undefined it'll search for undefined and it'll not give you any hint of in which part of the tree the error is.

Since your page is too complex to know where it is, or if this is even the problem you are facing; this is the most I can tell; the only reason I could find it too quick was because of react dev tools told me right away of my render properties and the fact that the component was quite small.



回答2:

One other way to find what is causing this issue (when you have a decent sized solution) is to check if there is any undefined assigned to any html attributes/styles. We can check this by right clicking on body tag, Expand recursively, copy the HTML, paste it and search for undefined.

If there is undefined assigned to href/src/url etc. it might give you a clue on what is wrong.

This can be fixed by not assigning undefined to those attributes.



回答3:

images not loading properly for React Redux Dynamic Image Location,

I got the error

Error: http://localhost/undefined 404 (Not Found)

so I tried below code its working

{
 user.id &&
 <img className="img-responsive" src={`${API_URL}/${imageLocation}`} />
}

So when get dynamic data after its render new data imagelocation api url.



回答4:

Many times it is fixed creating an .htaccess file with this content:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>