Search code examples
clojurehttp-kitcljs-ajax

Making an AJAX call when hosting on Localhost for HTTP-Kit


I am trying to make a front end and HTTP-Kit backend website. I ran into a lot of trouble with getting a response from the HTTP-Kit from within the web browser. When I access 'http://localhost:8080 from my we browser using HTTP-Kit webserver it works well and displays "hello HTTP!".

(defn app [req]
  {:ip "127.0.0.1"
   :status  200
   :headers {"Content-Type" "text/html"}
   :body    "hello HTTP!"})
(run-server app {:port 8080})

However, when I try to make an AJAX call to that same server using either Post or Get it no longer works and instead displays a status of 0.

I read it could have something to do with CORS compatibility but I am really at a loss for how to configure it to work.

Cheers,


Solution

  • In the headers section of the server side code, I had to put which allows cross domain sharing of files.

    :headers {"Content-Type" "text/html" "Access-Control-Allow-Origin" "http://localhost:8000"}
    

    and then this allowed me to make the AJAX request using cljs-http with

    (http/post "http://localhost:8080/" {:with-credentials? false})
    

    which allows the AJAX request.