react-native - 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")
)
如何解决此错误,我做错了什么?
解决方案
POST 时,您似乎收到 403 Forbidden 响应,GET 很好,但 POST 似乎被禁止。当服务器/客户端不在同一主机/源上运行时,可能会应用一些 CORS 限制。您在某处定义响应标头:
"Access-Control-Request-Method" "GET, OPTIONS"
也许在此响应标头中添加“POST”可能会解决这种情况。
推荐阅读
- python - NaN 值未替换为 dask 数据帧
- python - 使用 python-docx 将 docx 转换为 csv:合并单元格的问题
- python-3.x - 从 pexpect 查询中剥离字符的问题
- python - 打印遗传算法求解结果
- c# - 如何从 web.config 中提取自定义 XML 以在另一个类中使用?
- r - hist中的R广义双曲线分布图线
- pandas - 条形图随python中的熊猫而变化
- python - 我的 django 应用程序 url 反转不起作用
- python - 将行值更改为多索引
- react-window - React-window VariableSizeList 如何预先确定滚动条的大小?