【问题标题】:Axios get request works but post request returns callFunctionReturnedFlushedQueue and Network ErrorAxios 获取请求有效,但发布请求返回 callFunctionReturnedFlushedQueue 和网络错误
【发布时间】:2020-12-22 08:25:44
【问题描述】:

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

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

【问题讨论】:

  • 您收到“禁止”错误。您是否从同一服务器提供的 JS 页面运行您的 Axios 请求?请注意,您在 Access-Control-Request-Method 中仅指定“GET,OPTIONS”。
  • 指定帖子也不起作用。

标签: react-native clojure axios


【解决方案1】:

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

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

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

【讨论】:

  • 将 POST 添加到标头并不能解决错误
  • 在这里const res = await axios.post(domain + '/api/uploadFeatured');的axios帖子中,您执行了一个帖子但不提供任何帖子数据,也许这有影响?
  • 添加数据也不能解决问题
  • 您是否尝试过检查是否需要任何会话/身份验证 cookie?也许告诉 axios 发送凭据:const res = await axios.post(domain + '/api/uploadFeatured', data, { withCredentials: true })
猜你喜欢
  • 2018-08-14
  • 2018-04-19
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2019-02-25
  • 2022-09-25
  • 2020-03-06
相关资源
最近更新 更多