# HTTP.jl + Ajax

**URL:** <https://discourse.julialang.org/t/http-jl-ajax/68206>\
**Category:** Web Stack\
**Created:** [September 15, 2021, 10:54am UTC](https://discourse.julialang.org/t/http-jl-ajax/68206 "2021-09-15T10:54:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [September 15, 2021, 10:54am UTC](https://discourse.julialang.org/t/http-jl-ajax/68206/1 "2021-09-15T10:54:25Z")

</div>

Where can I find a simple example of a HTTP.jl backend server and a HTML+Javascript FE?  
I want to learn to communicate between Javascript and Julia for a simple app. Example: I would like to create a html input which takes a number `x`, uses Ajax to send the number to the Julia backend and then returns `x^2` in another html text, so I need to both serve and listen. The examples from HTTP.jl github page are separated between serve and listen.

I would like to do this barebones, without the aid of other frameworks like Genie or Dash.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [September 15, 2021, 11:53am UTC](https://discourse.julialang.org/t/http-jl-ajax/68206/2 "2021-09-15T11:53:17Z")

</div>

Check out  
[https://github.com/JuliaWeb/HTTP.jl/pull/757](https://github.com/JuliaWeb/HTTP.jl/pull/757)

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [September 15, 2021, 3:54pm UTC](https://discourse.julialang.org/t/http-jl-ajax/68206/3 "2021-09-15T15:54:17Z")

</div>

Thank you very much for your fast reply! Let’s say I save the html/javascript part to an `index.html` file. How can I serve that page? The example I used was:

```julia
HTTP.serve() do request::HTTP.Request
   try
       return HTTP.Response(200, read(raw"C:\myapp\index.html"))
   catch e
       return HTTP.Response(404, "Error: $e")
   end
end

```

But I don’t know how to adapt your example to that.

Thank you once more for your support!

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 15, 2021, 4:48pm UTC](https://discourse.julialang.org/t/http-jl-ajax/68206/4 "2021-09-15T16:48:11Z")

</div>

Register another route for GET /index.html, and the routed function will return the html content.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [September 15, 2021, 5:00pm UTC](https://discourse.julialang.org/t/http-jl-ajax/68206/5 "2021-09-15T17:00:17Z")

</div>

It worked! Thanks! The code:

```julia
using HTTP, JSON

const ROUTER = HTTP.Router()

function square(req::HTTP.Request)
    headers = [
        "Access-Control-Allow-Origin" => "*",
        "Access-Control-Allow-Methods" => "POST, OPTIONS"
    ]
    # handle CORS requests
    if HTTP.method(req) == "OPTIONS"
        return HTTP.Response(200, headers)
    end
    body = parse(Float64, String(HTTP.body(req)))
    square = body^2
    HTTP.Response(200, headers; body = string(square))
end

function render(req::HTTP.Request)
    HTTP.Response(200, read(raw"C:\index2.html"))
end

HTTP.@register(ROUTER, "POST", "/api/square", square)
HTTP.@register(ROUTER, "GET", "/index2.html", render)

HTTP.serve(ROUTER, "127.0.0.1", 8080)

```
