簡體   English   中英

使用Magento 2 REST API進行React Native。

[英]React Native, using Magento 2 REST API.

因此,我在本地主機上使用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>
    );
  }
}

我嘗試在線搜索解決方案,但沒有成功。 知道我在做什么錯。 任何幫助將不勝感激!

您可以嘗試使用這個網址嗎

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

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

您分享的信息尚不明確。 但是您可以使用React Native https://github.com/troublediehard/magento-react-native/blob/master/src/magento/index.js檢查我的Magento 2實現。 這個對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM