gpt4 book ai didi

javascript - “the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded” 来自 Android React Native 中的 super 代理

转载 作者:行者123 更新时间:2023-12-03 06:59:43 31 4
gpt4 key购买 nike

我正在使用 superagent从我的 React Native 应用程序上传文件。它在 iOS 上运行得非常好,但在 Android 上它给出了这个错误:

Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.



我在这里创建了一个最小的示例 https://snack.expo.io/@twohill/upload-example并复制下面的代码以防零食消失:

import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
import * as DocumentPicker from 'expo-document-picker';
import superagent from 'superagent';

import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';

const upload = (file, setMessage) => {
const { name } = file;

superagent.post('https://example.org/uploads') // <-- change this URL to your server that accepts uploads and is CORS enabled
.set('Authorization', 'BEARER xxxyyy') // <-- JWT token here
.attach('uri', file, name)
.then(
result => setMessage(JSON.stringify({result})),
error => setMessage(JSON.stringify({error}))
);
};

const pickDocuments = async (setMessage) => {
const file = await DocumentPicker.getDocumentAsync({ copyToCacheDirectory: true });
if (file.type === "success") {
upload(file, setMessage);
}
}

export default class App extends Component {
state = {
message: null,
}
render() {
const { message } = this.state;
return (

<View style={styles.container}>
<TouchableOpacity onPress={() => pickDocuments(message => this.setState({message}))}>
<Text style={styles.paragraph}>
Tap to upload a file
</Text>
<Card>
<AssetExample />
</Card>
<Card><Text>{message}</Text></Card>
</TouchableOpacity>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});


如果我 console.log 它给出以下错误:

Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
* http://192.168.1.3:19001/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&minify=false&hot=false:282311:24 in crossDomainError
- node_modules\@sentry\utils\dist\instrument.js:224:24 in <anonymous>
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:566:23 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

据我所知,在 Android 上,该应用从不尝试上传。
我的服务器运行 express并拥有 cors使用默认配置启用的中间件
{
"origin": "*",
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204
}

任何想法在这里做什么?我感觉 Android 对“*”起源犹豫不决,但不知道为移动应用程序放置什么。

还是我完全找错了树?

最佳答案

感谢大家的帮助。在确认错误不太可能是 CORS 后,我使用调试器进行了一些挖掘,发现了实际错误:Binary FormData part needs a content-type header
screenshot of XMLHttpRequest

从那里,我能够进行更多的挖掘,并发现我被文档 https://visionmedia.github.io/superagent/#attaching-files 误入歧途(至少在 Android 上)。作为 options我发送的 map 被忽略了

debugger showing underlying FormData append call

react-native-mime-types 的帮助下打包固定代码如下所示:

  //snip
const fileWithMime = { ...file, type: mime.lookup(name) };

request.post(`${SERVER_URL}/uploads`)
.set('Authorization', cookie)
.withCredentials()
.attach('uri', fileWithMime)

关于javascript - “the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded” 来自 Android React Native 中的 super 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60068177/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com