I have a small C server that has always the same respond:
"HTTP/1.1 200 OK\r\nServer: CServer\r\nAccess-Control-Allow-Headers: x-requested-with \r\nContent-Type: text/html\r\nContent-Length: 14\r\nConnection: close\r\n\r\n<h1>Done</h1>\n";
I can now call the webserver with my Browser and get the expected result, however, the same thing does not work with AJAX.
The Request is being sent and I also get the response back with the Response-Payload:
Done
But the error function is being executed: alert("An error occured: " + xhr.status + " " + xhr.statusText); with xhr.status = 0 and xhr.statusText = error, which is not really helping me.
Is my response-package incorrect? What do I need to change?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function(){
$.ajax({
type: "GET",
dataType : 'html',
url: "http://localhost:8080/demotest.txt",
success: function(result){
$("#div1").html(result);
},
error: function(xhr){
alert("An error occured: " + xhr.status + " " + xhr.statusText);
console.log(xhr);
}
});
});
});
</script>
</head>
<body>
<div id="div1"><h2>Before</h2></div>
<button id="button">Get Content</button>
</body>
</html>