【问题标题】:How to get status code of successful response in handler in cljs-ajax?如何在 cljs-ajax 的处理程序中获取成功响应的状态码?
【发布时间】:2017-04-12 08:39:22
【问题描述】:

我使用 cljs-ajax 成功地从端点获得响应(如下所示)。但是,我似乎无法区分响应处理程序中的不同成功状态代码。

(ns mynamespace
  (:require [ajax.core :as ajax]))

(defn start-monitoring []
  (let [handler (fn [[ok response]]
                  (if ok
                    (.log js/console response)
                    (.error js/console (str response))))]
    (ajax/ajax-request {:uri "/myendpoint"
                   :method :get
                   :params {:since (.getTime (js/Date.))}
                   :handler handler
                   :format (ajax/json-request-format)
                   :response-format (ajax/json-response-format {:keywords? true})})))

处理程序中的“ok”似乎只是一个真/假成功标志,并且不区分 200 和 204 状态代码,这两个状态代码都被视为成功。响应正文是响应中返回的任何文本,并且似乎不包含状态代码,除非请求失败。

如何确定响应的状态码?

【问题讨论】:

    标签: ajax clojure clojurescript http-status-codes


    【解决方案1】:

    似乎响应是带有 :status 之类的键的映射,其中包含 200 用于我的测试。 其余的键是: (:status :failure :response :status-text :original-text)

    【讨论】:

      【解决方案2】:

      使用:response-format (ajax/ring-response-format)。 另见:https://github.com/JulianBirch/cljs-ajax/issues/57

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-26
        • 1970-01-01
        • 2020-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多