How to send JSON between Elm Client and Haskell server (Yesod)

I am trying to get a basic JSON message from client to server, with the following Elm code:

import open Http result res = case res of Success a -> a Waiting -> "Waiting" Failure n a-> "Failure " ++ (show n) ++ " " ++ (show a) main = lift asText <| lift (show . result) <| send <| constant <| post "http://localhost:3000" "{foo : true}" 

The server is exactly as shown in this part of Yesod .

However, when I run the query, I get the output

 "\"Failure 0 []\"" 

Does anyone know what I need to do so that the Elm client communicates correctly with the Yesod server? I tried the Python client and the queries work fine. Similarly, there are several examples on the Yesod website with successful Http requests, so I am sure that both libraries are working properly, but I am using them incorrectly.

UPDATE: client side problem. I managed to get it to work with chrome with disabled security settings and no changes to Yesod. I will look for a workaround, but at least it is enough for my development to continue.

+7
json ajax haskell elm yesod
source share
2 answers

This is caused by the cross-site scripting protection feature on some sites. This was recently picked up on the Elm mailing lists (for me). There is currently no workaround if you intend to use your specific server, although I was lucky enough to host the files on elm-server , which contains all the files in the directory in which you ran it.

You should check the javascript console and you will see something like XMLHttpRequest cannot load http://www.google.com/. Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin. XMLHttpRequest cannot load http://www.google.com/. Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin. . If you do not, then this is a completely new problem. But I doubt it.

+7
source share

I assume your server needs to send CORS headers, which are discussed in the example in this SO post: Allow cross origin requests in Yesod

Learn more about CORS on MDN and Wikipedia.

Please note that the browser believes that you are executing Cross-Origin requests if

  • the domain name is different, for example, you are trying to extract data from foobar.com and you have barfoo.com in your browsers location
  • the subdomain is different, for example, your application runs at foobar.com and you are trying to extract data from api.foobar.com
  • the ports are different, for example, your browser application is located in localhost: 8000, and you are trying to extract data from localhost: 8001
+4
source share

All Articles