Axios get 请求有效但 post 请求返回 callFunctionReturnedFlushedQueue 和网络错误

时间:2020-12-22 08:25:44

标签: react-native clojure axios

我正在使用 axios 调用路由,我想真正将其作为发布请求调用。但是,当使用这样的 post 请求调用时:

export const uploadFeatured = (mediaName, youtubeLink, description) => async dispatch => {
    console.log("uploading", mediaName, youtubeLink, description);
    const res = await axios.post(domain + '/api/uploadFeatured');
}

我收到一个错误:

rror: Request failed with status code 403
createError@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:156601:26
settle@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:156591:25
handleLoad@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:156491:15
dispatchEvent@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:33005:31
setReadyState@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:32074:27
__didCompleteResponse@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:31905:29
emit@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:7758:42
__callFunction@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:3387:36
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:3119:31
__guard@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:3341:15
callFunctionReturnFlushedQueue@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:3118:21
callFunctionReturnFlushedQueue@[native code]

但是,get 请求可以正常工作,没有任何错误。这是我用于路由的 clojure 代码和用于请求处理的服务器:

(ns humboiserver.routes.home
  (:require
   [humboiserver.layout :as layout]
   [clojure.java.io :as io]
   [humboiserver.middleware :as middleware]
   [ring.util.response]
   [ring.util.http-response :as response]
   [humboiserver.routes.featured :as featured]))

(defn home-page [request]
  (layout/render request "home.html" {:docs (-> "docs/docs.md" io/resource slurp)}))

(defn about-page [request]
  (layout/render request "about.html"))

(defn home-routes []
  [""
   {:middleware [middleware/wrap-csrf
                 middleware/wrap-formats]}
   ["/" {:get home-page}]
   ["/api"
    ["/about" {:get about-page}]
    ["/featured" featured/get-featured]
    ["/invest" featured/invest]
    ["/connect" featured/connect]
    ["/uploadFeatured" featured/upload-featured]]])


(defn response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/edn"
             "Access-Control-Allow-Headers" "Content-Type"
             "Access-Control-Request-Method" "GET, OPTIONS, POST"
             "Access-Control-Allow-Origin" "*"
             "Access-Control-Allow-Credentials" true
             }
   :body (generate-string data)})


(defn upload-featured [req]
  (prn "request is " (:params req))
  ;;(db/insert "featured" (:params req))
  (response "uploaded")
  )

如何修复这个错误,我做错了什么?

1 个答案:

答案 0 :(得分:0)

您似乎在 POST 时收到 403 Forbidden 响应,GET 很好,但 POST 似乎被禁止。当服务器/客户端不在同一主机/源上运行时,可能会应用一些 CORS 限制。您在某处定义响应标头:

"Access-Control-Request-Method" "GET, OPTIONS"

也许向此响应标头添加“POST”可能会解决这种情况。