React Native,使用 Magento 2 REST API。

问题描述 投票:0回答:2

所以我在我的本地主机上使用 Magento 2 设置了一个电子商务网站。我想通过 React Native 中的 REST API 向 Magento 请求管理令牌。我尝试使用 fetch 方法,但无法继续,并且不断收到错误。我尝试从不同的 REST 客户端请求相同的东西,并且它在那里工作。错误是

    Network request failed
onerror
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\whatwg-fetch\fetch.js:441:29
dispatchEvent
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\event-target-shim\lib\event-target.js:172:43
setReadyState
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:567:29
__didCompleteResponse
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:397:25
<unknown>
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:503:16
emit
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:180:12
__callFunction
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:351:47
<unknown>
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:116:26
__guardSafe
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:314:6
callFunctionReturnFlushedQueue
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:115:17

这是我的代码:

import React from 'react';
import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

export default class FetchExample extends React.Component {

  constructor(props){
    super(props);
    this.state ={ isLoading: true}
  }

  componentDidMount(){
    return fetch('http://localhost/magento/rest/V1/integration/admin/token', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            "username": "RED_KAY",
            "password": "KGAMER30",
        }),    
    })
      .then((response) => response.json())
      .then((responseJson) => {

        this.setState({
          isLoading: false,
          dataSource: JSON.stringify(responseJson),
        }, function(){

        });

      })
      .catch((error) =>{
        console.error(error);
      });
  }
  render(){

    if(this.state.isLoading){
      return(
        <View style={{flex: 1, padding: 20}}>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
      <View style={{flex: 1, paddingTop:20}}>
        <Text> {this.state.dataSource} </Text>
      </View>
    );
  }
}

我尝试在网上寻找解决方案,但没有成功。我知道我做错了什么。任何帮助将不胜感激!

javascript react-native magento magento2 magento-rest-api
2个回答
0
投票

目前尚不清楚您分享的信息有什么问题。但是您可以使用 React Native 检查我的 Magento 2 实现https://github.com/troublediehard/magento-react-native/blob/master/src/magento/index.js。这个对我有用。


0
投票

你可以尝试使用这个网址吗

http://localhost/magento/index.php/rest/V1/integration/admin/token

来源:http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-token.html

© www.soinside.com 2019 - 2024. All rights reserved.