简体   繁体   English

如何从 React Native App 将我的长度变量发送到我的 HTTPS 服务器

[英]How do I send out my length variable to my HTTPS server from React Native App

I'm trying to use a slider bar function to produce an image out from the HTTPS server but I'm having issue with my fetch function.我正在尝试使用滑动条功能从 HTTPS 服务器生成图像,但我的提取功能有问题。 The App is supposed to have a slider which will determine the length variable, the variable will be sent to the server which will update and produce an image based of the length.该应用程序应该有一个滑块来确定长度变量,该变量将发送到服务器,服务器将根据长度更新并生成图像。

import { StatusBar } from 'expo-status-bar';
import React, {Component} from 'react';
import { StyleSheet, Text, View, Slider } from 'react-native';
import Dipole2 from './Components/Dipole2';


export default class App extends Component {
    constructor(props) {
    super(props);
    this.state = {
    length : 2
    } 
    
  };

  render() {
    return (
        <View style = {styles.LengthContainer}> 
  
       <Dipole2></Dipole2>
       <Text style={styles.number}> Length of Dipole </Text>
        <Text style={styles.number}> {this.state.length.toFixed(1)}</Text>
        <Slider 
        style = {{width: 300}}
          maximumValue={10}
          minimumValue={0.1}
          minimumTrackTintColor="#308ecc"
          maximumTrackTintColor="#000000"
          step={0.1} 
          value={this.state.length}
          onValueChange={length => this.setState({length})}
          
          />
      
      </View>
    );
    
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  LengthContainer: {
    flex: 400,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'flex-end',
    marginBottom: 200,

  },
  ImageContainer: {
    flex: 500,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
    marginBottom: 600,
    borderColor: 'black',
  },
  number: {

    fontSize: 25,
    textAlign: 'center',
    margin: 10,
  },
});

let data = {
  method: 'POST',
  body: ({ length }),
  headers: {

    'Content-Type': 'text/html',
    
  }
}
return fetch('https://dipolepattern.azurewebsites.net/api/HttpTrigger1', data)
        .then(response => response.json())  // promise
        .then(json => dispatch(receiveAppos(json)))

this is the other component that will hold the image这是将保存图像的另一个组件

import React from 'react';
import { Image } from 'react-native';



const Dipole2 = props => {
    let pic = {
     uri: 'https://dipolepattern.azurewebsites.net/api/HttpTrigger1'
    };
    return (
      <Image source={pic} style={{width: 193, height: 110, marginTop:50}}/>
    );
}

  
export default Dipole2;

The body field of the options passed to fetch must be a string.传递给fetchoptionsbody字段必须是字符串。 So you may replace this: body: ({ length }) by this : body: JSON.stringify({length}) .所以你可以用这个 : body: ({ length }) body: JSON.stringify({length})替换这个: body: ({ length }) body: JSON.stringify({length})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React Native Navigation 中,如何将道具发送到我的屏幕? - In React Native Navigation, how do I send props to my screens? Firebase 身份验证 - 如何构建我的反应原生应用程序? - Firebase Authentication - How do I structure my react native app? 如何将图像导入我的 React Native 应用程序? - How do I import an image into my React Native app? 如何在我的React应用程序中嵌入Facebook发送按钮? - How do I embed a facebook send button in my react app? 我如何让我的快递服务器从我的React客户端应用程序获取请求? - How do I get my express server to GET a request from my React client app? React Native:我可以在控制台上打印我的数据,但是如何在我的 React Native 应用程序屏幕上显示数据? - React Native: I am able to print my data on my console, but how do i display the data on my React Native app screen? 如何将令牌从反应发送到我的服务器? - How to send a token from react to my server? 如何从 web 应用程序/服务器发送数据以响应本机应用程序? - How to send data from web app/server to react native app? 如何从我的React应用发布到Facebook? - How do I post to facebook from my React app? 如何将通知从服务器发送到Android上的React Native应用? - How to send notifications from server to React Native app on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM