IE-11中script标签请求Node API重定向后挂起问题咨询
I’ve run into this exact IE11 compatibility quirk before—Chrome handles <script> tag 302 redirects seamlessly, but IE11 just chokes and hangs instead of following through to load the actual script. Let’s break down the likely causes and fixes:
1. Cross-Domain & CORS Headers Are Probably the Culprit
IE11 has far stricter rules for cross-domain script loading than modern browsers, especially when 302s jump to another server:
- Double-check that the server hosting
actual-script.jssends proper CORS headers, specificallyAccess-Control-Allow-Originset to your web app’s domain (or*if your use case allows open access). - IE11’s old XDomainRequest mechanism also blocks custom headers in cross-domain responses. If your Node API includes any custom headers in the 302 response, strip them out—they might be throwing IE11 off.
2. Skip the 302 Entirely (The Most Reliable Fix)
IE11’s <script> tag support for 302 redirects is notoriously flaky. Instead of redirecting, modify your Node API to proxy the script content directly:
- Have your API fetch
actual-script.jsfrom the target server, then send the raw script content back to the frontend with the correctContent-Type: application/javascriptheader. - Here’s a quick Express example:
app.get('/your-script-endpoint', async (req, res) => { try { const scriptRes = await fetch('https://other-server.com/actual-script.js'); const scriptContent = await scriptRes.text(); res.set('Content-Type', 'application/javascript'); res.send(scriptContent); } catch (err) { res.status(500).send('Failed to load script resource'); } });
3. Manually Handle Redirects with XHR for IE11
If you can’t change the API’s redirect behavior, add a browser-specific workaround:
- Detect IE11, then use XMLHttpRequest to fetch the script, manually follow the 302 redirect, and inject the script content into the page dynamically:
function loadAppScript(scriptUrl) { // Check for IE11 if (window.navigator.userAgent.includes('Trident/7.0')) { const xhr = new XMLHttpRequest(); xhr.open('GET', scriptUrl, true); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { // Inject the script content directly const scriptTag = document.createElement('script'); scriptTag.textContent = xhr.responseText; document.body.appendChild(scriptTag); } else if (xhr.status === 302) { // Follow the redirect manually loadAppScript(xhr.getResponseHeader('Location')); } } }; xhr.send(); } else { // Use standard script tag for modern browsers const scriptTag = document.createElement('script'); scriptTag.src = scriptUrl; document.body.appendChild(scriptTag); } } // Initialize the script load loadAppScript('/your-node-api-script-path');
4. Verify IE11 Security Settings
Occasionally, IE11’s default security policies block cross-domain redirects:
- Open IE11’s Internet Options > Security > Custom Level, then make sure "Access data sources across domains" is set to Enable or Prompt.
- For local development, add both your web app domain and the target script server to IE11’s Trusted Sites list.
内容的提问来源于stack exchange,提问作者Joel Lee

