1

I'm trying to implement the hack from this answer in a re-frame app.

It works in the sense that I actually get the confirmation email, but it throws an error and dispatches the :on-failure event.

The code:

(ns foo.events
  (:require
   [re-frame.core :as rf]
   [ajax.core :as ajax]
   [day8.re-frame.http-fx]))

(rf/reg-event-db :mc/yes
  (fn [db res] (prn res) db))

(rf/reg-event-db :mc/no
  (fn [db res] (prn res) db))

(rf/reg-event-fx :mc
  (fn [_ _]
    {:http-xhrio
     {:method :get
      :uri "https://XXX.us15.list-manage.com/subscribe/post-json?u=XXX&id=XXX&c=?"
      :format (ajax/json-request-format)
      :response-format (ajax/json-response-format)
      :params {"FNAME" "fooobar"
               "EMAIL" "some.dude@example.com"}
      :on-success [:mc/yes]
      :on-failure [:mc/no]}}))

(comment
  (rf/dispatch [:mc]) ; I execute this in my editor.
  )

The error:

Failed to load https://XXX.us15.list-
manage.com/subscribe/post-json?u=XXX&id=XXX&c=?
&FNAME=fooobar&EMAIL=some.dude@example.com: No 'Access-Control-
Allow-Origin' header is present on the requested resource. Origin 
'http://localhost:3449' is therefore not allowed access.

What's printed to the console:

[:mc/no {:uri "https://XXX.us15.list-manage.com/subscribe/post-json?u=XXX&id=XXX&c=?&FNAME=fooobar&EMAIL=some.dude@example.com", :last-method "GET", :last-error " [0]", :last-error-code 6, :debug-message "Http response at 400 or 500 level", :status 0, :status-text "Request failed.", :failure :failed}]

Edit:

This does what I want, except for depending on jquery. What does this do different than the http-xhrio version?

(defn mailchimp-signup! [data on-success on-failure]
  (-> js/$
      (.ajax #js{:type "get"
                 :data (clj->js data)
                 :url "https://XXX.us15.list-manage.com/subscribe/post-json?u=XXX&id=XXX&c=?"
                 :cache false
                 :dataType "json"
                 :contentType "application/json; charset=utf-8"
                 :error on-failure
                 :success on-success})))
madstap
  • 1,552
  • 10
  • 21
  • I suspect jQuery is including some extra headers that are making the difference. Can you provide the headers for both requests? (you can find them in the Network Inspector in Chrome or Firefox) – rafd May 18 '18 at 11:39

0 Answers0