如何在cljs-ajax中获得成功响应的状态代码?

时间:2021-12-24 14:23:05

I am successfully getting a response from an endpoint using cljs-ajax (as shown below). However, I cannot seem to differentiate between different success status codes in my response handler.

我正在使用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" in the handler appears to simply be a true/false success flag, and does not differentiate between 200 and 204 status codes, both of which are considered successes. The response body is whatever text is returned in the response, and doesn't appear to contain a status code, unless the request failed.

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

How can I determine the status code of the response?

如何确定响应的状态代码?

1 个解决方案

#1


2  

Seems like the response is a map with keys like :status which contains 200 for my test. The rest of the keys are: (:status :failure :response :status-text :original-text)

似乎响应是一个带有键的映射,比如:status,在我的测试中包含200。其余的键是:(:status:failure:response:status-text:original-text)

#1


2  

Seems like the response is a map with keys like :status which contains 200 for my test. The rest of the keys are: (:status :failure :response :status-text :original-text)

似乎响应是一个带有键的映射,比如:status,在我的测试中包含200。其余的键是:(:status:failure:response:status-text:original-text)