react native中使用fetch做get请求和post请求

get请求:

import React, { useState, useRef, useEffect } from 'react'
import { View, TextInput, Text, Button } from 'react-native'
import style from './static/style'

export default function App() {
  const [username, setUsername] = useState('admin')
  const [password, setPasswork] = useState('123456')
  const usernameEl = useRef(null)

  const handleInput = (e) => {
    console.log(e)
    setUsername(e)
  }

  const handleLogin = () => {
    console.log(777, username, password)
    fetch('http://10.3.138.173:81/api/getUserInfo').then(res => res.json()).then(res => {
      console.log(res)
    })
  }

  useEffect(() => {
    //usernameEl.current.focus()
    //console.log(666, usernameEl.current.isFocused())
  }, [])

  return (
    
      
        
      
      
        
      
      
        
      
    
  )
}

post请求:

import React, { useState, useRef, useEffect } from 'react'
import { View, TextInput, Text, Button } from 'react-native'
import style from './static/style'

export default function App() {
  const [username, setUsername] = useState('admin')
  const [password, setPasswork] = useState('123456')
  const usernameEl = useRef(null)

  const handleInput = (e) => {
    console.log(e)
    setUsername(e)
  }

  const handleLogin = () => {
    console.log(777, username, password)
    fetch('http://10.3.138.173:81/api/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({username, password})
    }).then(res => res.json()).then(res => {
      console.log(res)
    })
  }

  useEffect(() => {
    //usernameEl.current.focus()
    //console.log(666, usernameEl.current.isFocused())
  }, [])

  return (
    
      
        
      
      
        
      
      
        
      
    
  )
}

react native中使用fetch做get请求和post请求_第1张图片

 

你可能感兴趣的:(react native中使用fetch做get请求和post请求)