troubleshooting Question

Simple ajax with cross domain error

Avatar of Steve Tinsley
Steve TinsleyFlag for United Kingdom of Great Britain and Northern Ireland asked on
HTMLAJAX
5 Comments1 Solution108 ViewsLast Modified:
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>

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??
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros