GET 404错误排查求助:NLP情感检测项目前后端交互异常
Hey there! Let’s troubleshoot that 404 error together—nothing’s more frustrating than a broken API call when you’re trying to get your project working. Let’s walk through the most likely issues in your code and setup:
1. Fix the Frontend API Request Path & Parameter Encoding
Looking at your JavaScript code, the request URL is built as:
xhttp.open("GET", "emotionDetector?textToAnalyze"+"="+textToAnalyze, true);
Two key issues here:
- Relative Path Ambiguity: When your frontend page loads from the root URL (
http://localhost:5000/), using a relative path likeemotionDetectormight work in some cases, but it’s safer to use an absolute path to avoid URL context confusion. Update it to:xhttp.open("GET", "/emotionDetector?textToAnalyze=" + encodeURIComponent(textToAnalyze), true); - Unencoded User Input: If users enter text with spaces,
&,?or other special characters, it’ll break the request parameters. WrappingtextToAnalyzeinencodeURIComponent()ensures the input is properly formatted for URLs.
2. Correct the Static File Loading Path
In your index.html, the script tag uses a relative path:
<script src="../static/mywebscript.js"></script>
Since your Flask app serves static files from the static folder by default, and your index page loads from the root (/), the browser will interpret ../static as trying to go up a directory from the root (which doesn’t exist). Switch to an absolute path instead:
<script src="/static/mywebscript.js"></script>
If the JS file isn’t loading correctly, the RunSentimentAnalysis function might not execute at all—this is a quick check to ensure your frontend logic is even running.
3. Confirm Your Project File Structure
Your server.py imports emotion_detector from EmotionDetection.emotion_detection, which means your project needs this exact folder structure:
your-project/ ├── server.py ├── EmotionDetection/ │ ├── __init__.py # Required for Python to recognize this as a module (can be empty) │ └── emotion_detection.py ├── templates/ │ └── index.html └── static/ └── mywebscript.js
If emotion_detection.py is in the same folder as server.py instead of the EmotionDetection subfolder, your server will fail to start with an import error—meaning there’s no backend to handle the request, hence the 404.
4. Verify the Flask Server is Running Properly
Make sure you’ve started the server with:
python server.py
Look for output like this in your terminal:
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
If the server crashes on startup (e.g., due to the import issue above), there’s no service listening for your frontend requests—any call to /emotionDetector will automatically return a 404.
5. Inspect the Request in Browser DevTools
Open your browser’s Developer Tools (F12), go to the Network tab, then click the "Run Sentiment Analysis" button. Check the failed GET request:
- Look at the Request URL to confirm it matches
http://localhost:5000/emotionDetector?textToAnalyze=your-input-text. If it’s something likehttp://localhost:5000/templates/emotionDetector..., that confirms the relative path issue. - Check the Response tab for any extra details about the 404 (though Flask’s default 404 page might not give much extra context).
Try these fixes one by one—most likely the path encoding or file structure is the culprit. Let me know if any of these work, or if you run into new errors we can tackle!
备注:内容来源于stack exchange,提问作者Chasey Lorraine

