我正在使用fetch带有React Native的API.
{"message": "error here"}如果状态> = 400,我的响应遵循正常格式,我将在本机弹出窗口中显示.
我response.json()在检测到故障后试图打电话,但它仍然把所有东西都放在一个奇怪的格式中......
{ _45: 0, _81: 0, _65: null, _54: null }
无论出于何种原因...我想要的实际响应位于_65......我不知道这些随机键是什么.
所以目前我不得不通过它访问它_bodyText,但我认为这是错误的,因为它是一个私有的下划线方法.
我究竟做错了什么?
var API = (function() {
var base = 'https://example.com/api/v1';
var defaults = {
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
var alertFailure = function(response) {
if (response.status >= 200 && response.status < 400) {
return response;
} else {
var json = JSON.parse(response._bodyText || '{}');
var message …Run Code Online (Sandbox Code Playgroud) 我正在使用Fetch API发出HTTP请求.
fetch(url, options)
.then(response =>
console.log(...response.headers.keys())); // "content-type"
Run Code Online (Sandbox Code Playgroud)
但我可以从Chrome看到入站HTTP响应中有更多标头(日期,服务器,传输编码,X-foo).为什么我可能无法在输出中看到它们到上面的代码?
我正在使用以下标志运行Chrome:
C:\path\chrome.exe --disable-web-security --user-data-dir=C:\path\tmp\chrome
Run Code Online (Sandbox Code Playgroud)
响应:
body: (...)
bodyUsed: false
headers: Headers
ok: true
status: 200
statusText: "OK"
type: "cors"
url: "http://origin?query"
Run Code Online (Sandbox Code Playgroud)
我注意到,发出非CORS请求会保留标头.CORS剥离它们吗?
body: (...)
bodyUsed: false
headers: Headers
ok: true
status: 200
statusText: "OK"
type: "basic"
url: "http://origin?query"
Run Code Online (Sandbox Code Playgroud) 我在请求类型似乎正在更改的地方抓到我的帖子。我提交基本表格(仅一个字段)。这是提取。
handleSubmit(event, data) {
//alert('A name was submitted: ' + this.state.value);
event.preventDefault();
console.log("SUBMIT STATE::", this.state.value);
return (
fetch("//localhost:5000/api/values/dui/", {
method: "post",
mode: 'no-cors',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
name: this.state.value,
})
}).then(response => {
if (response.status >= 400) {
this.setState({
value: 'no greeting - status > 400'
});
throw new Error('no greeting - throw');
}
return response.text()
}).then(data => {
var myData = JSON.parse(data);
this.setState({
greeting: myData.name,
path: myData.link
});
}).catch(() => {
this.setState({ …Run Code Online (Sandbox Code Playgroud) 我试图在使用Fetch API向服务器发出每个请求后运行一些简单的JS函数.我已经搜索了这个问题的答案,但是没有找到任何答案,可能是因为Fetch API是相对较新的.
我一直这样做XMLHttpRequest:
(function () {
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
this.addEventListener('load', function () {
someFunctionToDoSomething();
});
origOpen.apply(this, arguments);
};
})();
Run Code Online (Sandbox Code Playgroud)
很高兴知道是否有办法使用Fetch API完成同样的全局事务.
在这里,我有这个样的小提琴,在那里我试图让仅在content-type从一个大的图像头,同时使用XMLHttpRequest()和fetch()在Javascript。如果您在打开Chrome的“开发人员工具/网络”标签(或其他浏览器中的类似工具)的情况下运行该小提琴,则会看到该XMLHttpRequest()方法仅获得标头所在的600 B,但该fetch()方法获得了整个7.8 MB的图像,尽管我的代码只需要content-type标题。
如何使用Fetch APi获得标头所需的几个字节(而不是整个内容)fetch()?
注意:我对此感兴趣,因为很显然,如果我尝试获取常规Google页面的所有链接的标题,该XMLHttpRequest()方法将使我注销,有时还会更改我的语言,而该fetch()方法则不会。我认为这是因为前者发送凭据/ Cookie等,而后者默认情况下更具“限制性” ...
使用react-fetch-intercept拦截所有请求并添加标头的正确方法是什么?我有一个配置文件,其中包含对fetchIntercept.register()的调用。我为导入fetchIntercept配置文件的组件api调用提供了单独的文件。我在调用api之后添加了unregister()调用,但未添加标头。
api / config.js
import fetchIntercept from 'fetch-intercept';
const unregister = fetchIntercept.register({
request: function (url, config) {
// Modify the url or config here
const withDefaults = Object.assign({}, config);
withDefaults.headers = defaults.headers || new Headers({
'AUTHORIZATION': `Bearer ${JSON.parse(sessionStorage.credentials).authToken}`
});
return [url, withDefaults];
},
requestError: function (error) {
// Called when an error occured during another 'request' interceptor call
return Promise.reject(error);
},
response: function (response) {
// Modify the reponse object
return response;
},
responseError: function (error) {
// Handle an …Run Code Online (Sandbox Code Playgroud) 我正在使用reactjs构建仅前端的基本Weather App。对于API请求,我正在使用Fetch API。在我的应用程序中,我从找到的简单API获取当前位置,并且该位置将其作为JSON对象给出。但是,当我通过Fetch API请求它时,出现此错误。
Failed to load http://ip-api.com/json: Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.
Run Code Online (Sandbox Code Playgroud)
因此,我进行了搜索,找到了解决此问题的多种解决方案。
那么,如何才能永久解决该问题?我可以用来解决Fetch API中HTTP请求的CORS问题的最佳解决方案是什么?
得益于HTTP / 2的“ 每个来源一个连接”的原理,浏览器可以在加载网页时(脚本,图像等)并行加载许多资源,但是脚本应如何动态地执行相同的操作?
例如,如果脚本创建5个XHR对象并并行调用同一服务器,它们是否都将重用同一TCP连接?如果脚本需要通过Ajax从同一服务器请求100个资源怎么办?它应该创建100个单独的XHR对象还是应该创建少量并在每次请求完成时重用它们?如果是这样,那么在HTTP / 2时代有多少个最优选择?
通常,很难找到有关XHR(以及较新的Fetch API)浏览器正在使用HTTP / 2进行操作的低层详细信息,以及查找浏览器在页面加载时执行操作的最佳方法的确切信息,即尽快加载数十个小资源。
如果没有数据,有没有发现错误的机会?我收到错误404,但无法通过console.log例如...
class App extends React.Component{
getWeather = async (e) => {
e.preventDefault();
const city = e.target.elements.city.value;
const country = e.target.elements.country.value;
const api_call = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${API_KEY}&units=metric`);
const data = await api_call.json();
console.log(data);
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>Run Code Online (Sandbox Code Playgroud)

我正在学习Ramda并试图达到无点编程.为了做到这一点,我尝试在这里和那里进行重构但是却坚持这一点.
我显然认为这不起作用,因为调用是异步的,但我找不到这个代码有什么问题.
// Why is this
const toJSONRamda = R.pipe(
R.prop('json'), // getting the 'json' function
R.call // and calling it
)
// different from this
const toJSON = response => response.json()
// Works
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(toJSON)
.then(console.log)
// Does not Work
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(toJSONRamda)
.then(console.log)Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>Run Code Online (Sandbox Code Playgroud)
javascript functional-programming promise ramda.js fetch-api
fetch-api ×10
javascript ×8
reactjs ×4
cors ×2
ajax ×1
async-await ×1
fiddler ×1
http ×1
http-headers ×1
http2 ×1
performance ×1
promise ×1
ramda.js ×1
react-native ×1