相关疑难解决方法(0)

使用 fetch 进行基本身份验证(或任何身份验证)

无法找到任何相关文档,因此在我深入研究代码之前,是否有人知道在使用“fetch”发出 REST 请求时如何使用基本身份验证(https://github.com/github/fetch)。

\n\n

刚刚尝试了以下行,但请求中未设置标头:

\n\n
  fetch(\'http://localhost:8080/timeEntry\', {\n      mode: \'no-cors\',\n      headers: { \'Authorization\': \'Basic YW5kcmVhczpzZWxlbndhbGw=\' }\n    })\n    .then(checkStatus)\n    .then(parseJSON)\n    .then(function(activities) {\n      console.log(\'request succeeded with JSON response\', data);\n      dispatch(activitiesFetched(activities, null));\n    }).catch(function(error) {\n      console.log(\'request failed\', error);\n      dispatch(activitiesFetched(null, error));\n    });\n
Run Code Online (Sandbox Code Playgroud)\n\n

用户名和密码是我自己的名字和姓氏,使用curl它就可以了。

\n\n

如果我设置了{\xc2\xa0\'Accept\' : \'application/test\' }接受,只是不Authorization……奇怪了。

\n\n

只是为了我能够继续我添加credentials: \'include\'这使得浏览器提示输入用于与 REST 后端通信的用户名和密码。仅用于测试,稍后将使用 OAuth。

\n\n
  fetch(\'http://localhost:8080/timeEntry\', {\n      mode: \'no-cors\',\n      credentials: \'include\'\n    })\n    .then(checkStatus)\n    .then(parseJSON)\n    .then(function(activities) {\n      console.log(\'request succeeded with JSON response\', data);\n      dispatch(activitiesFetched(activities, null));\n …
Run Code Online (Sandbox Code Playgroud)

javascript rest

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

标签 统计

javascript ×1

rest ×1