重定向302在我从Express传递给它时不起作用

时间:2019-02-27 09:38:14

标签: javascript reactjs express axios

我有一个Express应用程序,在我的API中,我向客户端发送302状态。客户端使用React和AXIOS。它引发此错误:

import React from 'react';
import { Image, StyleSheet, Button, Text, View, Alert, } from 'react-native';
import { ImagePicker } from 'expo';
import * as firebase from 'firebase';
import {firebaseConfig} from "./ApiKeys";

export default class HomeScreen extends React.Component {
    static navigationOptions = {
        header: null,
    };

    onChooseImagePress = async () => {
        let result = await ImagePicker.launchCameraAsync();
        //let result = await ImagePicker.launchImageLibraryAsync();

        if (!result.cancelled) {
            this.uploadImage(result.uri, "test-image")
            .then(() => {
                Alert.alert("Success");
            })
            .catch((error) => {
                Alert.alert(error);
            });
        }
    }

    uploadImage = async (uri, imageName) => {
        const response = await fetch(uri);
        const blob = await response.blob();

        var ref = firebase.storage().ref().child("images/" + imageName);
        return ref.put(blob);
    }

    render() {
        return (
            <View style={styles.container}>
            <Button title="Choose image..." onPress={this.onChooseImagePress} />
            </View>
            );
        }
    }

    const styles = StyleSheet.create({
        container: { flex: 1, paddingTop: 50, alignItems: "center", },
    });
}

这是我的API

Access to XMLHttpRequest at 'https://sandbox.zarinpal.com/pg/StartPay/000000000000000000000000000000012807' (redirected from 'http://localhost:5000/api/payment/x/handle/accepted') 
from origin 'null' has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource

这是React的一部分

router.post('/x/handle/accepted', (req, res) => {
    const uuidv4 = require('uuid/v4');
    zarinpalLib.request(req.body.acamount, req.body.acemail, req.body.acphone, req.body.acdescription, uuidv4(), function (data) {
        if (data.status) {
            console.log('here ', data.url)
            res.writeHeader(302, { 'Location': data.url });
            res.end();
        } else {
            console.log('here2 ', data.code)
            // res.render('error', {zpTitle: appConfig.appTitle, zpError: data.code});
            res.status(500).send(data.code)
        }
    });
});

我在Express应用程序中将此中间件用于CORS:

if (inputValue === 'RLS') {
    var data = {
        acfirstname: 'fars',
        aclastname: '2p',
        acemail: 'rajaby.amir@gmail.com',
        acphone: phoneNumber,
        acamount: inputAmount,
        acdescription: 'fars2p'
    };

    const res = await;
    axios({ method: 'POST', url: `${END_POINT_URL}/api/payment/x/handle/accepted`, data });
    console.log(res)
} 

0 个答案:

没有答案