fetch连接AZURE数据库

之前用学习用react-native开发手机app新生项目,实现app的基本操作,其中最重要的一项就是用fetch (https://fetch.spec.whatwg.org/#responses)连接到远程数据库,我看了半天资料也没看懂,在网上也没找到实例代码,最后看了队友的代码后才明白,看来还是太年轻。

 

废话少说,直接看代码(以下为硬代码):

'use strict'
import React,{Component}from 'react'
import {props}from 'react-native'
import backToSignUp from './SignUp'
const http= 'http://dytestfirst.azurewebsites.net/tables/' //数据库url/tables
const id='ff96a1f6-a5c4-4bf3-a3bb-0d7f998c74db'
export default class DataBase
{
toInsert( user, pwd,email)
{
    fetch('http://dytestfirst.azurewebsites.net/tables/User', {  //表名
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'ZUMO-API-VERSION':'2.0.0'
        },
        body: JSON.stringify({    //字段
            username: user,
            password: pwd,
            MAIL: email
        })
    }).then((response) => response.text())
        .then((responseText) => {
            console.log(responseText);
        })
        .catch((error) => {
            console.warn(error);
        });
}

updatePWD(password)//修改
{
    fetch(http+'user/'+id, {
        method: "PATCH",
         headers: {
         "Accept": "application/json",
         "Content-Type": "application/json",
         "ZUMO-API-VERSION":"2.0.0",
         },
         body:JSON.stringify
         ({
            PASSWORD: password,
         })
         }).then((response) => response.text())
         .then((responseText) => {
         console.log(responseText);
         })
         .catch((error) => {
         console.warn(error);
         });
         }

delete()//删除
 {
     fetch(http+'user/'+id, {
     method: "DELETE",  //删除
     headers: {
     "ZUMO-API-VERSION":"2.0.0",
     },
     }).then((response) => response.text())
     .then((responseText) => {
     console.log(responseText);
     })
     .catch((error) => {
     console.warn(error);
     });

     }

getPassword()
{
    fetch(http+'user/'+id, {
        method: "GET",
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            "ZUMO-API-VERSION": "2.0.0"
        },
    }).then((response) => response.text())
        .then((responseText) => {
            console.log(responseText);
        })
        .catch((error) => {
            console.warn(error);
        });
      router.pop();

}

/*
    async getasswordFromApi()
    {
        try {
            let response = await fetch('http://dytestfirst.azurewebsites.net/tables/User?ZUMO-API-VERSION=2.0.0');
            console.log(response.password);
            return response.password;
        } catch(error) {
            throw error;
        }
    }
*/
}
文章目录
|