jQuery Ajax GET请求无法进入.done函数,触发fail弹窗求助
Hey there! Let's figure out why your Ajax call is triggering the fail() handler instead of done()—even though you see content in the browser console. Here are the most likely fixes to try:
1. Fix the Authorization Header Format
Your current Authorization: "Basic" is incomplete. Basic auth requires a base64-encoded string of username:password appended right after "Basic ". If this header is malformed, the server will reject the request (often with a 401 error), which automatically triggers the fail() handler.
Update the header like this (replace your-username and your-password with your actual credentials):
headers: { "Authorization": "Basic " + btoa("your-username:your-password"), "Cache-Control": "no-cache" }
2. Check for JSON Parsing Errors
You set dataType: "application/json", which tells jQuery to automatically parse the response as JSON. If the server returns invalid JSON (even if it has visible content), jQuery will fail the request and jump straight to fail().
Test this by:
- Temporarily changing
dataTypeto"text"to see ifdone()fires (you can then log the response to check its actual format) - Adding
console.log(jqXHR.responseText)in thefail()handler to inspect the raw server output
3. Force Disable Request Caching
Even with Cache-Control: no-cache, browsers sometimes still cache GET requests. Add jQuery's built-in cache: false parameter to your Ajax config—it appends a unique timestamp to the URL to bypass caching entirely:
$.ajax({ type: "GET", url: "your-url-here", dataType: "application/json", cache: false, // Add this line headers: { /* ... */ } })
4. Dig Deeper with Error Details
Modify your fail() handler to log detailed error information—this will tell you exactly what's going wrong (status code, error type, raw server response):
.fail(function(jqXHR, textStatus, errorThrown) { alert("Error: " + textStatus + " - " + errorThrown); console.log("Full error object:", jqXHR); console.log("Raw server response:", jqXHR.responseText); });
5. Verify CORS Configuration (Even with the Plugin)
Double-check the network request in Firefox's DevTools (Network tab):
- Look at the request's status code (is it 200 OK, or a 4xx/5xx error?)
- Check the response headers for
Access-Control-Allow-OriginandAccess-Control-Allow-Headers—make sure they include your origin and the headers you're sending (Authorization,Cache-Control)
Modified Full Code Example
Here's a revised version of your script with all these fixes included:
function data() { $.ajax({ type: "GET", url: "your-actual-url", dataType: "application/json", cache: false, headers: { "Authorization": "Basic " + btoa("username:password"), "Cache-Control": "no-cache" } }).done(function(response) { alert("Success!"); console.log("Server response:", response); }).fail(function(jqXHR, textStatus, errorThrown) { alert("Sorry, server unavailable. Error: " + textStatus); console.log("Error details:", textStatus, errorThrown); console.log("Raw server response:", jqXHR.responseText); }); }
Start with checking the error details first—it'll give you the clearest clue about what's breaking the request.
内容的提问来源于stack exchange,提问作者Doggo

