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

RESTlet本地XHR请求失败:直接访问URL正常

Troubleshooting RESTlet + Dojo XHR Status 0 Error

Let’s work through this frustrating issue you’re facing—direct URL access works, mobile requests over SSH port forwarding work, but local Dojo XHR calls keep throwing that status 0 error. Here’s a breakdown of the most likely causes and actionable fixes:

First, Let’s Recap Your Problem

Directly visiting http://localhost:8080/asWeb/r/WebVersion returns valid JSON. Mobile Dojo XHR requests work perfectly via SSH port forwarding, but local requests fail with:

STATUS: 0 (RequestError: Unable to load http://localhost:8080/asWeb/r/WebVersion status: 0)
message: "Unable to load /asWeb/r/Login status: 0"

1. You’re Running the Frontend via file:// Protocol (Most Common Fix)

If you’re opening your local HTML file directly in the browser (like double-clicking it), you’re using the file:// protocol. Browsers enforce strict same-origin policy here, which blocks requests to http://localhost:8080—since file:// and http:// are considered completely separate origins. The mobile setup avoids this because the app is likely running in a web view or on a server that aligns with the forwarded endpoint’s origin.

Fix:
Host your frontend files on a local web server instead of using file://. Easy options include:

  • Use a simple Node.js server: Install http-server via npm (npm install -g http-server), run http-server in your frontend folder, and access it at http://localhost:8081 (or another free port).
  • Use Apache/Nginx if you already have them set up.
  • This ensures your frontend runs on the http:// protocol, matching your backend’s protocol and resolving same-origin conflicts.

2. Missing CORS Configuration in RESTlet

Even on localhost, requests between different ports (e.g., frontend on 8081, backend on 8080) trigger cross-origin resource sharing (CORS) checks. Mobile web views often have looser policies, but desktop browsers enforce this strictly.

Fix:
Add a CORS filter to your RESTlet application to explicitly allow cross-origin requests:

import org.restlet.ext.cors.CorsFilter;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class YourWebApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        // Add your existing route configurations here (e.g., /asWeb/r/WebVersion, /asWeb/r/Login)
        
        // Set up CORS filter
        CorsFilter corsFilter = new CorsFilter(getContext(), router);
        corsFilter.setAllowedOrigins("*"); // Replace with specific origins (like http://localhost:8081) for production
        corsFilter.setAllowedMethods("GET,POST,PUT,DELETE,OPTIONS");
        corsFilter.setAllowedHeaders("Content-Type,Authorization");
        
        return corsFilter;
    }
}

This tells your RESTlet backend to send the necessary headers to permit frontend requests from different ports.

3. Browser Plugins or Security Settings Blocking the Request

Desktop browser extensions like ad blockers, privacy tools, or antivirus software sometimes intercept local XHR requests, resulting in a status 0 error with no clear explanation.

Fix:

  • Test your request in the browser’s incognito/private mode (plugins are disabled by default here).
  • Temporarily turn off all browser extensions and re-run the request.
  • Check the browser’s DevTools console (F12 > Console tab) for hidden error messages about blocked requests.

4. Double-Check Your Request Path

The error mentions /asWeb/r/Login but you tested /asWeb/r/WebVersion—make sure your Dojo code’s request path is accurate. If you’re using a relative path, it might resolve incorrectly based on your frontend’s URL structure.

Fix:
Use the full absolute URL in your Dojo XHR call to avoid path confusion:

dojo.xhrGet({
    url: "http://localhost:8080/asWeb/r/WebVersion",
    handleAs: "json",
    load: function(response) {
        console.log("Success:", response);
    },
    error: function(err) {
        console.error("Error:", err);
    }
});

内容的提问来源于stack exchange,提问作者user3260912

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:03:06