在React Native中获取请求发布方法 [英] Fetch request post method in React Native

查看:42
本文介绍了在React Native中获取请求发布方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过post方法使它满足提取请求的要求,并且获得其他api调用效果很好.这是我的代码:

I cannot make it work the fetch request with the post method, with get other api calls works great.This is my code:

return fetch(URL_LOCAL + `/addLike`, {
    method: 'post',
    body: JSON.stringify({a: 1})
})
    .then(response => Promise.all([response, response.json()]))
    .catch(err => {
        return Promise.reject(err);
    })

在获取有关URL_LOCAL的信息之前,我做了一个console.log,它返回 http://localhost:5000 .

I made a console.log before the fetch about the URL_LOCAL and its fine it returns http://localhost:5000.

它给了我这个错误:

Network request failed
    at XMLHttpRequest.xhr.onerror (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:15825)
    at XMLHttpRequest.dispatchEvent (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:17902)
    at XMLHttpRequest.setReadyState (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:17657)
    at XMLHttpRequest.__didCompleteResponse (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:17484)
    at blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:17594
    at RCTDeviceEventEmitter.emit (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:3480)
    at MessageQueue.__callFunction (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:2386)
    at blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:2156
    at MessageQueue.__guardSafe (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:2348)
    at MessageQueue.callFunctionReturnFlushedQueue (blob:http://localhost:8081/459df9ca-c7b8-48bc-9c45-03d2f1e94f1d:2155)

我正在使用chrome开发工具的android模拟器上查看日志,并且我正在使用node和mongodb作为后端,但我在通话开始时放了一条日志,但未触发

I'am on an android simulator using chrome dev tools to see the logs, and i'am using node and mongodb as backend , but i put a log in the beginning of the call but is not fired

我可以正常使用,但是生产中的api与我在localhost:5000上使用的代码相同,我不知道localhost可能有什么问题

I make it work but with the api in production is the same code that i used with localhost:5000, I dont know what can be wrong with the localhost

推荐答案

可以尝试用以下代码替换代码:

Can you try replacing your code with following:

return fetch(URL_LOCAL + `/addLike`, {
    method: 'post',
    body: JSON.stringify({a: 1})
})
    .then(response => Promise.resolve(response.json()))
    .catch(err => {
        return Promise.reject(err);
    })

这篇关于在React Native中获取请求发布方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆