You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTTP 405 Method Not Allowed及CORS问题求助(EPSON TM U220)

Fixing Epson TM U220 CORS & 405 Method Not Allowed Issues (JavaScript SDK)

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:

  1. Origin: null means you're opening your HTML file directly via file:// protocol, which browsers treat as an extra-restrictive case for CORS.
  2. The OPTIONS pre-flight request is getting a 405 because the printer's CGI endpoint only allows GET/HEAD/POST methods. Browsers send this pre-flight automatically when your request uses non-simple headers (like soapaction or custom Content-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-server globally 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:

  1. 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);
  1. 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 usually admin/admin or 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:50:03