Avatar of Steve Tinsley
Steve Tinsley
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Simple ajax with cross domain error

I want to be able to fire a simple HTML request using ajax.
I don't have access to change the server I am requesting, but all it does is fire back OK as plain text if all is ok.

Here is my super simple code:

<!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({
         url: "http://127.0.0.1:8000/press/bank/1/1",
         success: function(data)
         {
            console.log(data);

            if (data == "ok") {
               $("#div1").html(data);
            } else {
               alert("not ok :( - " + data);
            }
         },
         error: function(jqXHR, textStatus, errorThrown)
         {
            console.log("Oops: " + textStatus + errorThrown);
         }

      });
      return false;
   });


});


</script>

</head>

<body>

<div id="div1"></div>

<button>button</button>

</body>
</html>

Open in new window


The issue is that i get an error response:

Access to XMLHttpRequest at 'http://127.0.0.1:8000/press/bank/1/1' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
untitled.html:36 Oops: error

When I look at the network tab in Chrome I still get the OK back....

Is there a way to avoid this error??
HTMLAJAX

Avatar of undefined
Last Comment
Steve Tinsley

8/22/2022 - Mon