标签: fetch-api

获取api - 在两者中获取json body并捕获块以获取单独的状态代码

我使用fetch api来获取可能返回的URL:

回复:状态= 200,json body = {'user':'abc','id':1}

要么

回复:状态= 400,json body = {'reason':'某些原因'}

要么

回复:状态= 400,json body = {'reason':'其他原因'}

我想创建一个单独的函数request(),我从代码的各个部分使用如下:

    request('http://api.example.com/').then(
        // status 200 comes here
        data => // do something with data.id, data.user
    ).catch(
        // status 400, 500 comes here
        error =>  // here error.reason will give me further info, i also want to know whether status was 400 or 500 etc
    )
Run Code Online (Sandbox Code Playgroud)

我无法进行200到400,500之间的分割(我试过抛出一个错误).当我抛出错误时,我发现很难仍然提取JSON主体(用于error.reason).

我目前的代码如下:

import 'whatwg-fetch';

/**
 * Requests a URL, returning a promise
 */
export …
Run Code Online (Sandbox Code Playgroud)

javascript promise es6-promise fetch-api

1
推荐指数
1
解决办法
4870
查看次数

调用返回401

如果我运行以下内容:

fetch('http://localhost:8080/root/1487171054127/k_query_bearer_token',{mode: 'no-cors'}).
then(response=>response)
Run Code Online (Sandbox Code Playgroud)

从我通过localhost托管的webapp中:11111我收到401错误.

但是,如果我在同一个浏览器中打开一个选项卡并输入http:// localhost:8080/root/1487171054127/k_query_bearer_token,那么我得到200响应.

我究竟做错了什么?

UPDATE

这是通过卷曲工作:

卷曲' 的http://本地主机:8080/GUI /根/ 1487171054127/k_query_bearer_token ' -H '的Cookie:JSESSIONID = CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5'

所以我需要提供一个cookie

javascript fetch-api

1
推荐指数
1
解决办法
2583
查看次数

为什么在我的单页应用程序中获取错误没有堆栈跟踪?

我有两个简单的包装器来处理我的单页应用程序中的请求。如果响应不正确(不在 200-300 范围内),则包装fetch并抛出错误:

const fetchy = (...args) =>
  fetch(...args).then(response => {
    if (response.ok) {
      return response
    }

    throw new Error(response.statusText)
  })

export default fetchy
Run Code Online (Sandbox Code Playgroud)

一个包装 fetchy 并用于 GET 请求:

const get = endpoint => {
  const headers = new Headers({ Authorization: `Bearer ${TOKEN}` })
  const init = { method: 'GET', headers }

  return fetchy(endpoint, init)
}
Run Code Online (Sandbox Code Playgroud)

现在我在这样的动作中使用它们(这是一个redux-thunk动作创建者):

export const fetchArticles = () => dispatch => {
  dispatch({ type: types.FETCH_ARTICLES })

  return get(endpoints.ARTICLES)
    .then(response => response.json())
    .then(data …
Run Code Online (Sandbox Code Playgroud)

javascript error-handling stack-trace reactjs fetch-api

1
推荐指数
2
解决办法
1398
查看次数

如何使用 fetch API 取回数组?

我有一个 API,它将返回一个数组给我。我尝试使用 fetch API 来取回数组。我知道如果我使用 fetch 获取一些数据,响应中的真实主体是 ReadableStream。我通常对付它通过response.json()then功能,如果数据是JSON。我不知道的是如何处理数组数据?

javascript fetch-api

1
推荐指数
1
解决办法
1万
查看次数

如何使用 Fetch API 从表单中检索和发送数据?

我有 html 表单:

<form action="file.php" method="post">
    <input name="formName" type="text" />
    <input name="formEmail" type="email" />
    <input name="formSubmit" type="submit" value="Submit Me!" />
</form>
Run Code Online (Sandbox Code Playgroud)

那么如何使用 Fetch API 来获取这些值并使用 ajax 将它们发送到 file.php 文件呢?

html javascript php ajax fetch-api

1
推荐指数
1
解决办法
5079
查看次数

如何在react-redux中使用获取来发送数据

我正在执行多个复选框过滤器,并且必须使用post方法将一个对象发送到服务器。我如何使用提取来做到这一点?我可以使用提取发送数据吗?从服务器我必须收到一个过滤列表。谢谢!

reactjs redux fetch-api

1
推荐指数
1
解决办法
2998
查看次数

在fetch事件中使用respondWith和waitUntil

是否有必要使用waitUntil内部a respondWith(本身在fetch事件中)?不respondWith已经waitUntil接收到一个解决的承诺?

这里有一些讨论,其中给出了以下简单示例,其中使用了两个:

addEventListener('fetch', event => {
  event.respondWith(
    fetch(whatever).then(response => {
      event.waitUntil(addThisToTheCache(response));
      return response;
    })
  );
});
Run Code Online (Sandbox Code Playgroud)

但是没有这个可以写waitUntil吗?如下:

addEventListener('fetch', event => {
  event.respondWith(
    fetch(whatever).then(response => {
      return addThisToTheCache(response).then(() => {
        return response;
      });
    })
  );
});
Run Code Online (Sandbox Code Playgroud)

javascript service-worker fetch-api

1
推荐指数
1
解决办法
814
查看次数

fetch()函数中的捕获错误

我最近学到了一些关于fetch()和promise的知识,现在我需要在项目中使用它.这里我有一个fetch()函数,它运行得很好,但我想,它必须捕获一个错误.那么,在fetch()函数中捕获错误的最佳方法是什么?我需要在两个()中捕获它们?这里有一些代码:

const endpoint = 'http://localhost:3030/api/hotels';
const promise = fetch(endpoint)
   .then(res => res.json(), err => {
      console.log(err);
   })
   .then(parseRooms, err => {
      console.log(err);
   })
Run Code Online (Sandbox Code Playgroud)

谢谢 !

javascript error-handling fetch es6-promise fetch-api

1
推荐指数
1
解决办法
1602
查看次数

如何将域URL保存在单个文件中并在不同组件中重用?

所以,我有一个域名网址,我使用不同的路径来向api发出不同的请求.所以例如"api.example-url.com",我使用不同的路径,如"api.example-url.com/path1"和"api.example-url.com/path2",所以我想保存域URL在单个文件中,然后将其导入不同的组件,以便我可以使用不同的路径,每当域更改时,我只需更改该文件中的域.reactjs有可能吗?

ecmascript-6 reactjs fetch-api

1
推荐指数
1
解决办法
42
查看次数

fetch()和window.fetch()有什么区别?

参见标题。在我们的react项目中调用fetch时,最初是从fetch到window.fetch使用的(现已离开)开发人员。我不确定两者之间的区别,也无法在网上找到任何确定性的内容(W3Schools甚至没有提到fetch是窗口的功能)

javascript reactjs fetch-api

1
推荐指数
1
解决办法
65
查看次数