HTTP 405 Method Not Allowed及CORS问题求助(EPSON TM U220)
Got it, let's break down what's happening and tackle this step-by-step—since you can't modify the printer's firmware, we'll focus on changes you can make to your code or deployment setup.
First, Understand the Root Causes
From your error details:
- Origin: null means you're opening your HTML file directly via
file://protocol, which browsers treat as an extra-restrictive case for CORS. - The
OPTIONSpre-flight request is getting a 405 because the printer's CGI endpoint only allowsGET/HEAD/POSTmethods. Browsers send this pre-flight automatically when your request uses non-simple headers (likesoapactionor customContent-Type), which the Epson SDK relies on for HTTP-based requests.
Solution 1: Use the Epson SDK's WebSocket Connection (Not Direct CGI Calls)
The ePOS SDK is designed to use WebSocket (port 8008) by default, which avoids CORS issues entirely—WebSocket connections don’t trigger browser pre-flight requests the same way HTTP does. Make sure you’re using the official SDK API instead of manually calling the service.cgi endpoint:
// Initialize the device with WebSocket port (8008) const printer = new epson.ePOSDevice(); // Connect to the printer's WebSocket service printer.connect('192.168.199.15', 8008, (connectionResult) => { if (connectionResult === 'OK') { // Open the printer device (use 'local_printer' as devid) printer.openPrinter('local_printer', (openResult) => { if (openResult === 'OK') { // Example print job printer.addText('Test print from JavaScript!\n'); printer.addCut(); printer.send((sendResult) => { console.log(sendResult === 'OK' ? 'Print successful!' : 'Print failed'); // Clean up connection printer.closePrinter(); printer.disconnect(); }); } else { console.error('Failed to open printer:', openResult); } }); } else { console.error('Failed to connect:', connectionResult); } });
Double-check that your printer has WebSocket enabled (most TM U220 models do by default). You can verify by visiting http://192.168.199.15:8008 in your browser—you should see a socket.io welcome response.
Solution 2: Host Your Code on a Local HTTP Server
If you're opening your HTML file directly (file://), switch to hosting it on a simple local server to fix the Origin: null problem:
- Python: Run this in your project folder:
# Python 3 python -m http.server 8080 # Python 2 python -m SimpleHTTPServer 8080 - Node.js: Install
http-serverglobally first:npm install -g http-server http-server -p 8080
Then access your app via http://localhost:8080/your-file.html—this gives you a valid Origin value, which plays nicer with the printer's security rules.
Solution 3: Package Your App as a Desktop Application (Bypass Browser CORS)
If WebSocket isn't working and you still hit CORS blocks, package your JavaScript app into a desktop app using tools like Electron. Desktop apps don't enforce browser CORS restrictions:
- Set up a basic Electron project, then add this to your main process file (
main.js):
const { app, BrowserWindow } = require('electron'); function createMainWindow() { const win = new BrowserWindow({ width: 600, height: 400, webPreferences: { webSecurity: false, // Disable CORS checks contextIsolation: false, // To access SDK in renderer process nodeIntegration: true } }); win.loadFile('index.html'); // Your app's HTML file } app.whenReady().then(createMainWindow);
- Run the app with
electron .—this will let your code communicate with the printer without CORS errors.
Solution 4: Verify Printer Settings
Quick check to rule out printer-side configuration issues:
- Log into the printer's web admin panel at
http://192.168.199.15(default credentials are usuallyadmin/adminor blank) - Ensure "ePOS SDK Settings" are enabled, and WebSocket port 8008 is open
- Confirm the printer is on the same network as your device
内容的提问来源于stack exchange,提问作者Raj

