RESTlet本地XHR请求失败:直接访问URL正常
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/WebVersionreturns 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-servervia npm (npm install -g http-server), runhttp-serverin your frontend folder, and access it athttp://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

