无法找到任何相关文档,因此在我深入研究代码之前,是否有人知道在使用“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 });\nRun Code Online (Sandbox Code Playgroud)\n\n用户名和密码是我自己的名字和姓氏,使用curl它就可以了。
如果我设置了{\xc2\xa0\'Accept\' : \'application/test\' }接受,只是不Authorization……奇怪了。
只是为了我能够继续我添加credentials: \'include\'这使得浏览器提示输入用于与 REST 后端通信的用户名和密码。仅用于测试,稍后将使用 OAuth。
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 }).catch(function(error) {\n console.log(\'request failed\', error);\n dispatch(activitiesFetched(null, error));\n });\nRun Code Online (Sandbox Code Playgroud)\n
由于看起来您正在使用的库是Fetch API的 polyfill ,因此我将基于语法也应该执行的假设进行工作。
我在 Mozilla 页面上找到的示例表明fetch方法签名fetch('API_ENDPOINT', OBJECT)如下object所示:
myHeaders = new Headers({
"Authorization": "Basic YW5kcmVhczpzZWxlbndhbGw="
});
var obj = {
method: 'GET',
headers: myHeaders
})
Run Code Online (Sandbox Code Playgroud)
所以该方法就变成了:
fetch('http://localhost:8080/timeEntry', obj)
.then(checkStatus)
.then(parseJSON)...
Run Code Online (Sandbox Code Playgroud)
我没有测试过这段代码,但它似乎与我能找到的一致。希望这能为您指明正确的方向。
| 归档时间: |
|
| 查看次数: |
22580 次 |
| 最近记录: |