I'm having a problem with my ajax displaying the response in an alert.
I use jquery and ajax. The javascript contains a normal GET-Request. The ajax doesn't go to the .done function. The website displays the second alert:
alert("Sorry. Server unavailable. ");
But in the browser it displays in the console. I'm using firefox + a Plug-in for Firefox (https://addons.mozilla.org/de/firefox/addon/cors-everywhere/reviews/). I don't think the plugin is the proplem because without the plugin it's also working.
So here is my html:
<html>
<head>
<meta charset="utf-8" />
<title>Peyer E1 9.2 Auswahl</title>
<script src="clearcache.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<button onclick="data()">Test</button>
</body>
</html>
Here is my Javascript:
function data() {
$.ajax({
type: "GET",
url: "url",
dataType: "application/json",
headers: {
"Authorization": "Basic",
"Cache-Control": "no-cache"
}
}).done(function() {
alert("Success.");
}).fail(function() {
alert("Sorry. Server unavailable. ");
});
}
If you're getting a 200 result from the Ajax request then it's failing when it's attempting to parse the response.
Change the dataType
value in the Ajax request to "json". It's currently "application/json", which is not a valid data type...
function data() {
$.ajax({
type: "GET",
url: "url",
dataType: "json", // make sure this is correct
headers: {
"Authorization": "Basic",
"Cache-Control": "no-cache"
}
}).done(function() {
alert("Success.");
}).fail(function() {
alert("Sorry. Server unavailable. ");
});
}