抱歉这个基本问题。我正在尝试使用 fetch 从 api 获取数据,并希望将该响应存储到 javascript 变量中,但它没有像我预期的那样工作 我的代码-
async function fun() {
var a = "initial";
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(res => res.json())
.then(data => {
a = data;
console.log(a);
})
await console.log("Response => ", a);
}
Run Code Online (Sandbox Code Playgroud)
输出 - 响应 => 初始
这样做的正确方法是什么。
当附加一个新域时,我做了一个逻辑,通过 fetch 检查该域是否正常工作。
fetch("https://" + domain).then((result)=>{
const isOk = result.status === 200
...
})
Run Code Online (Sandbox Code Playgroud)
但是,我认为通过 fetch 获取整个 HTML 文档只是为了检查它是否有效,效率很低。我怎样才能只获取状态值?
我正在开发一个 FastAPI 应用程序,该应用程序需要对用户访问的某些端点进行身份验证。我正在使用 FastAPI 中的 Oauth2 和 Jose 为我的身份验证过程创建 JWT。经过一些研究后,确保令牌在前端受到保护的最佳方法似乎是将它们存储在 HttpOnly Cookie 中。我正在努力了解如何通过 HttpOnly Cookie 正确传递 JWT,以便我的 FastAPI 服务器能够验证标头中的 JWT。目前,当我尝试将 JWT 令牌作为 HttpOnly Cookie 传递时,我得到一个401 Unauthorized Error.
当我将令牌作为模板字符串编码到标头中时,我已经能够使用 JWT 令牌成功地对用户进行身份验证。但是,当我通过标头将 JWT 作为 Cookie 传递到 FastAPI 服务器时,我的 FastAPI 服务器无法对用户进行身份验证并返回401 unauthorized error. 我尝试查看网络选项卡,看看我的请求中向 FastApi 服务器发送了哪些标头,以便更好地了解这两种情况之间的不同之处。
当我将 JWT 作为模板字符串传递并获得 200 响应时,这是在标头中:
身份验证:不记名令牌
async function getPosts() {
const url = "http://localhost:8000/posts";
const fetchConfig = {
headers: {
Authorization: `Bearer ${tokenValue}`,
},
};
const response = await fetch(url, …Run Code Online (Sandbox Code Playgroud) 我是使用CoreData的新手,我正在尝试了解如何在表上执行查询.我可以使用获取请求从表中提取所有记录,但我正在寻找一个子集.是否有捷径可寻?
谢谢,豪伊
我的项目基于React,redux,redux-saga,es6,我尝试从此API提取数据:
如您所见,此特定的API调用显示的数据限制为每页100个数据,分布在40个页面上。
根据此答案: http ://userforum.dhsprogram.com/index.php?t=msg&th=2086&goto=9591&S=Google它表示您可以将限制扩展到每页最多3000个数据。
但是,在某些情况下,我会进行超出该限制的API调用,这意味着我不会像这样接收所有数据:
export function fetchMetaData(countryCode: string, surveyYears: string) {
return (fetch('http://api.dhsprogram.com/rest/dhs/data/' + countryCode + ',' + surveyYears + '?returnFields=CharacteristicLabel,Indicator,IndicatorId,Value&f=json')
.then(response => response.json())
.then(json => json.Data.map(survey => survey)))
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是;考虑到我知道数据的总页数,从此API获取所有数据的最佳方法是什么。论坛链接中的答案建议循环浏览API。但是,我找不到正确的语法用法来做到这一点。
我的想法是进行一次api调用以获取页面总数。然后使用redux + redux-saga将其存储在状态中。然后执行一个新请求,将总页数作为参数发送,并获取此总页数。通过这样做,我无法弄清楚存储每次迭代数据的语法。
我正在开发一个维基百科查看器,我正在尝试从维基百科API中提取一些数据.这应该是一个正常的请求,任何想法为什么这个方法没有给出任何响应?我正在使用fetch库.行console.log(数据)根本不运行.
function getArticleList() {
var searchFor = "";
searchFor = document.getElementById('intext').value;
console.log(searchFor);
fetch("https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchFor + "&limit=5").then(function(resp) {
console.log("trySearch");
return resp.json()
}).then(function(data) {
console.log(data);
document.querySelector.artName.innerText = data.object[1];
document.querySelector.textArt.innerText = data.object[0];
document.querySelector.href = data.object[2]
})
};
Run Code Online (Sandbox Code Playgroud) 我希望这里有人可以帮我解决这个问题。我一直在为解决这个错误而斗争太久了。
我正在向传感器的API发出提取请求,这始终导致相同的错误:JSON解析错误:意外的EOF
我试图在网上找到答案,并尝试了很多建议,但是都没有用。
在开发环境中,我正在通过本地网络访问API。
fetchUsers(timer) {
let data = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer ' + this.state.token.access_token
}
}
fetch('http://' + this.state.ip + ':5000/api/Devices/031.01.0657/LiveState', data)
.then(response => response.json())
.then(responseJson => {
this.setState({
isLoading: false,
error: false,
userDataSource: responseJson,
refreshing: false,
time: timer
}, function () {
// If success, do something (I never make it to this part)
});
})
.catch(error => {
this.setState({
isLoading: false,
error: true
})
console.error(error);
});
} …Run Code Online (Sandbox Code Playgroud) 谁能告诉我为什么从硬盘驱动器访问数据时,此代码为什么能在HTML页面中完美地工作,但是当我将其添加到express和node中时,却得到了
SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符
具有完美格式的代码。我知道我使用格式化程序对其进行了测试,甚至手动创建了一个json对象。这是代码:
<html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Untitled</title>
</head>
<body>
<div id="output"></div>
<button id="getProperty">Get Property</button>
<script>
document.getElementById('getProperty').addEventListener('click', getProperty);
function getProperty() {
fetch('2016-regular.json')
.then((res) => res.json())
.then((data) => {
let output = '<h2>Property</h2>';
console.log(data);
data.propertystandings.propertystandingsentry.forEach(function(propertyEntry){
output += `
<ul>
<li>id: ${propertyEntry.property.ID}</li>
<li>city: ${propertyEntry.property.City}</li>
<li>prop name: ${propertyEntry.property.Name}</li>
<li>prop name: ${propertyEntry.rank}</li>
</ul>
`;
});
document.getElementById('output').innerHTML = output;
})
}
</script>
</body>
</html>
</html>
</html>
But then this code in express causes the error- …Run Code Online (Sandbox Code Playgroud) 如何使用Visual Studio 17从现有的Bitbucket GIT存储库中提取信息?
错误“ git失败并出现致命错误。找不到'xyz'存储库 ”
首先,我尝试了此字符串-> https://<repo_owner>@bitbucket.org/<accountname>/<reponame>.git&https://<repo_owner>@bitbucket.org/<accountname>/<reponame> 然后尝试了基于youtube视频和over设置页面的URL路径https://bitbucket.org/myrepo/xos/src
希望能从Bitbucket设置/拉取/获取/同步代码方面获得一些帮助。
更新2:@ Marina-Liu,即使单击connect后,bitbucket连接也不会显示
在Visual Studio 2017中
邮差请求似乎工作,但我不能让fetch()使用相同的请求和标头.它让我疯狂.
客户:
fetch('http://localhost:1234/acts/create', {
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: JSON.stringify({
name: 'BARNEY MCGREW!',
rating: 90,
})
})
Run Code Online (Sandbox Code Playgroud)
表达:
exports.act_create = function (req, res) {
console.log(' req >>>>', req.body);
var act = new Act(
{
name: req.body.name,
rating: req.body.rating
}
);
// res.set('Content-Type', 'application/x-www-form-urlencoded');
act.save(function (err) {
if (err) {
return console.log(err);
}
res.send('Act Created successfully')
})
};
Run Code Online (Sandbox Code Playgroud)
这会生成以下终端输出:
req >>>> : [Object: null prototype] { '{"name":"IngleburtHumperdink","rating":10}': '' }
act is: { _id: 5c4245bea7bb511c20de6b7a }
Run Code Online (Sandbox Code Playgroud)
所以它有点过来,但后来我得到了ValidationError: Act …