美文网首页
Fetch网络请求

Fetch网络请求

作者: 喜剧收尾_XWX | 来源:发表于2020-09-08 22:10 被阅读0次

http请求

方式一

postData('http://example.com/answer', {answer: 42})
  .then(data => console.log(data)) // JSON from `response.json()` call
  .catch(error => console.error(error))

function postData(url, data) {
  // Default options are marked with *
  return fetch(url, {
    body: JSON.stringify(data), // must match 'Content-Type' header
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, same-origin, *omit
    headers: {
      'user-agent': 'Mozilla/4.0 MDN Example',
      'content-type': 'application/json'
    },
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, cors, *same-origin
    redirect: 'follow', // manual, *follow, error
    referrer: 'no-referrer', // *client, no-referrer
  })
  .then(response => response.json()) // parses response to JSON
}

方式二

loadData2(){

    let url = `https://api.github.com/search/repositories?q=${this.searchKey}`;
    fetch(url)
      .then(response => {

        console.log("response: " + response);
        if(response.ok){
          return response.text();
        }
        throw new Error ("服务没有响应");

      })
      .then(responseText => {
        this.setState({

          showText:responseText
        })
      })
    
      .catch(e => {
        alert(e.toString());
         this.setState({
          showText:e.toString
         })
      })

  }

相关文章

  • React Native学习之路(5) -网络请求,ListVi

    Fetch,WebSocket,XMLHttpRequest (1) fetch网络请求 (2) mock.js来...

  • Fetch网络请求

    http请求 方式一 方式二

  • React-Native之Fetch

    网络请求是开发过程中比不可少的一个环节。在React-Native中,使用fetch实现网络请求。fetch同XM...

  • fetch的使用

    fetch是js提供进行网络请求的框架。 调用结构是这样的。 fetch( url , options ).the...

  • ReactNative开发之网络请求

    在ReactNative中,使用fetch实现网络请求。fetch同XMLHttpRequest非常类似,是一个封...

  • 初学Fetch

    什么是 Fetch API? Fetch 提供了 Request 和 Response对象(以及与网络请求有关的其...

  • React学习补充

    React 网络请求 方法一 原生请求,react自带的fetch请求方式: 方法二 ajax请求,react通过...

  • React Naitve 网络请求

    React Native 网络 [React-Native Fetch方法发送网络请求](http://www.q...

  • 网络请求(ajax,fetch)

    ajax 本文所讲并不考虑过低版本的浏览器,所以对于ajax对象的创建并没有进行处理。上一段简单的代码 如此便可以...

  • React-Native Fetch网络请求

    在react-native开发中,使用Fetch进行网络请求。官方文档上的网络请求 基本使用方法 GET请求 ca...

网友评论

      本文标题:Fetch网络请求

      本文链接:https://www.haomeiwen.com/subject/liacohtx.html