0

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>
Drimer
  • 93
  • 2
  • 14
  • Please take the [tour](https://stackoverflow.com/tour) and learn [how to ask a good question](https://stackoverflow.com/help/how-to-ask). "Does not work" is not a clear problem statement. – Quentin Sep 26 '18 at 15:54
  • What happens when you run that code? Use the developer tools in your browser. Look at the Console to see if there are errors. Look at the Network tab to see if the request is made, is formatted as you expect, and gets the response you expect. – Quentin Sep 26 '18 at 15:55
  • It looks like you have two problems, the first being a typo (which checking the Network tab should highlight to you), and the second being a duplicate of [this](https://stackoverflow.com/a/35553666/19068). – Quentin Sep 26 '18 at 15:58
  • Sorry. I changed my question. The Response is visible in my browser developer tools and also the "Access-Control-Allow-Headers: *" in my response is not helping. – Drimer Sep 26 '18 at 16:31
  • Look in the console of the browser's developer tools. What does the error message say? – Quentin Sep 27 '18 at 08:37

0 Answers0