我正在使用React Native开发Android应用程序.我正在使用在Mac上运行的Android Studio中模拟的Galaxy Nexus API 23上测试我的应用.
我希望允许用户彼此共享信息.我正在运行一个带有php和mysql的web服务器,我希望应用程序向服务器发出请求,以便在用户之间共享数据.
我正在尝试使用Fetch API向我的开发服务器发出POST请求.我的js代码如下:
var AjaxEngine = {
test: function() {
return fetch('http://10.0.2.2/test.json')
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
}
};
Run Code Online (Sandbox Code Playgroud)
叫做 AjaxEngine.test();
在我的模拟器中,我收到以下错误:
ExceptionsManager.js:70 TypeError:XMLHttpRequest.xhr.onerror(http:// localhost:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:23711:8)的XMLHttpRequest.dispatchEvent 网络请求失败(http:// localhost:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:9740:15)XMLHttpRequest.setReadyState(http:// localhost:8081/index.android.bundle?platform = android&dev =真热=假缩小=假:25157:6)在XMLHttpRequest的.__ didCompleteResponse(HTTP://本地主机:8081/index.android.bundle平台=机器人&dev的=真热=假缩小=假:25015:6)在HTTP://本地主机:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = …
请问,如何将fetch的输出保存到变量中 - 能够像对象一样使用它?
这是代码:
var obj;
fetch("url", {
method: "POST",
body: JSON.stringify({
"filterParameters": {
"id": 12345678
}
}),
headers: {"content-type": "application/json"},
//credentials: 'include'
})
.then(res => res.json())
.then(console.log)
Run Code Online (Sandbox Code Playgroud)
决赛console.log将展示一个对象.但是当我试图将它保存到变量.then(res => obj = res.json())而console.log(obj)不是保存Object时,却是Promise.
请问,如何把它变成保存在变量中的对象?
then(res => res.json())react-native提取中的以下代码段是什么意思?
fetch(url)
.then(res => res.json())
.then(res => {
this.setState({
data: res,
error: res.error || null,
loading: false
});
Run Code Online (Sandbox Code Playgroud) 说明:我正在做一个反应本机应用程序,在那里我有我下面GitHub上的用户列表,我想实现取消追踪和刷新列表的功能.
我创建了两个异步帮助程序来与github API进行交互,一个用于取消关注用户(通过PUT),另一个用于获取以下列表(通过GET).我还在以下组件列表中添加了一个firebase侦听器.以下每个人都会将我导航到一个由取消关注按钮组成的个人资料视图.单击按钮时,它应取消关注用户,更新组件中的以下列表,然后导航回以下组件列表.
问题 取消关注用户正在按预期工作,但以下列表视图仍包含旧列表.我的代码返回旧数据,即使github api正在返回新的更新数据,所以我怀疑问题必须是我使用async/await的方式.
我甚至做了一个刷新按钮来刷新以下列表,但新数据有时只返回,比如1/20次.
更新:在测试多个场景之后,我认为firebase不是问题,因为fetch返回相同的旧数据.我认为主要问题可能在于fetch电话会议.我已经测试了从Postman获取数据并且它抓取了正确的数据.
fetch由于response.json()包含旧数据,似乎没有按预期工作.我在这里做了一个简单的jsfiddle(你需要提供自己的访问令牌),显示get_following -> unfollow -> get_following成功运行,也就是修改了以下数据.但是,在我的应用程序中,即使github网站UI显示更改并且Postman返回新修改的数据fetch,unfollow也会返回相同的旧的后续数据.我还稍微更新了代码.
码
以下列表
/**
* Listens for any changes on the database and updates the
* dataSource accordingly.
* @param {Firebase Object} ref
*/
_listenForData(ref) {
ref.on('value', (snapshot) => {
this.setState({
dataSource: snapshot.val()
},
this.setState({
isLoading: false,
}));
});
}
componentDidMount() {
// Sets up the listener for realtime changes.
GithubApi.get_followings(this.ref) …Run Code Online (Sandbox Code Playgroud) 我正在开发我的第一个react / redux应用程序,我不确定应该在哪里调用sessionStorage.setItem()。我目前正在从loginUserSuccess()操作存储用户凭证,但是我不确定这是应该执行的操作。此外,我正在使用访存来发出请求,并希望将用户的authToken添加到所有请求中。我一直在研究fetch-intercept,但没有提供太多文档来修改标头。
actions / loginActions.js
export function loginUser(user) {
return function(dispatch) {
return LoginApi.login(user).then(creds => {
dispatch(loginUserSuccess(creds));
}).catch(error => {
throw(error);
});
};
}
export function loginUserSuccess(creds) {
sessionStorage.setItem('credentials', JSON.stringify(creds));
return {
type: types.LOGIN_USER_SUCCESS,
state: creds
}
}
Run Code Online (Sandbox Code Playgroud)
api / packageApi.js
class PackageApi {
// called on successful login
static getAllPackages() {
const request = new Request('/my/endpoint', {
method: 'GET',
headers: new Headers({
'AUTHORIZATION': `Bearer ${JSON.parse(sessionStorage.credentials).authToken}`
})
});
return fetch(request).then(response => {
return response.json();
}).catch(error => { …Run Code Online (Sandbox Code Playgroud) 简而言之,当使用带有 fetch api 的授权标头并遵循重定向时,Chrome、FireFox 和 Opera(在 Mac 上)在第二个请求中包含授权标头。但是,Safari (12.0.1) 没有。从fetch api 规范和问题 #553我的理解是应该包含标题。这是对 fetch 应该如何工作的正确理解吗?
这是我用于 Safari 的解决方法的简化代码,但我想知道是否我做错了什么导致了 Safari 的行为,或者是否有更好的解决方法。
export async function loadData(token) {
const opts = {headers: {Authorization: `Bearer ${token}`}, credentials: 'include', redirect: 'follow'};
let response = await fetch('/api/data', opts);
// Work around for Safari not including headers in redirected request
if (response.status === 401 && response.redirected) {
response = await fetch(response.url, opts);
}
if (response.ok) {
return response.json();
}
return null; …Run Code Online (Sandbox Code Playgroud) 我有 Python Flask (v 1.0.0) 后端,它返回状态为 400 的 http 响应和自定义错误消息
return Response('My custom errror message', 400)
Run Code Online (Sandbox Code Playgroud)
在我的前端,我使用 Fetch API 对后端进行了 API 调用:
fetch(url, options)
.then(response => response.json())
.then(response => console.log(response))
.catch(error => console.log(error));
Run Code Online (Sandbox Code Playgroud)
由于 400 是一个BAD REQUEST,fetch直接进入catch,我所拥有的只是TypeError: Failed to fetch消息。
如何在前端获取自定义错误消息而不是Failed to fetch消息?它在 DevTools 中可见,所以我希望它应该以fetch某种方式可用?
背景:我正在使用 Shopify ScriptTag,它允许我在店面添加 JavaScript 文件。我只有那个脚本文件。
当前行为:有一个选项“立即购买”,允许客户跳过Add To Cart直接结账。当他们点击一口,Shopify发送取() POST请求checkouts.json创建结账。
问题:我需要在我自己的 JavaScript 文件中检测这个“获取请求发生”。
self.addEventListener('fetch', event => {
console.log("event happened");
});
Run Code Online (Sandbox Code Playgroud)
我试过Fetch Event API,但它似乎只在Service Worker范围内工作。
有没有可能检测到这一点?
就像我们可以通过使用原型继承覆盖其open方法来检测XMLHttpRequest一样。
我正在尝试向此端点发送请求:https://9gag.com/v1/group-posts/group/default/type/trending获取 9gag 帖子数据。
在邮递员和 chrome 上运行良好,但是当我尝试axios从 Node.js使用时,它响应403(并读取返回的 DOM,它认为它要求我输入验证码),当我尝试使用 发送请求时node-fetch,它响应与200帖子数据(这是正确的)。
问题是,为什么node-fetch工作正常但axios没有?
这是我用于测试的代码:
import axios from "axios";
import fetch from "node-fetch";
const URL = "https://9gag.com/v1/group-posts/group/default/type/trending";
// Inside async function
await fetch(URL); // Responded with 200 and json data
await axios(URL); // Responded with 403 and HTML DOM, axios.get() also gives the same result
Run Code Online (Sandbox Code Playgroud) 更新到 Angular 17 后出现此错误:
NG02801:Angular 检测到“HttpClient”未配置为使用“fetch” API。强烈建议为使用服务器端渲染的应用程序启用“fetch”,以获得更好的性能和兼容性。要启用“fetch”,请将“withFetch()”添加到应用程序根目录的“provideHttpClient()”调用中。
我认为在对我的服务进行编码并HttpClient通过注入函数导入之后,它达到了这一点。
fetch-api ×10
javascript ×6
react-native ×3
ajax ×1
android ×1
angular ×1
async-await ×1
axios ×1
fetch ×1
firebase ×1
http ×1
httpclient ×1
jsx ×1
node-fetch ×1
node.js ×1
react-redux ×1
reactjs ×1
redirect ×1
safari ×1
webkit ×1