首页 > 解决方案 > Axios 获取请求有效,但发布请求返回 callFunctionReturnedFlushedQueue 和网络错误

问题描述

我正在使用 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]

但是,获取请求没有任何错误。这是我的路由和请求处理服务器的 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")
  )

如何解决此错误,我做错了什么?

标签: react-nativeclojureaxios

解决方案


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

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

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


推荐阅读