我正在使用 nativescript 为 android 开发应用程序。我有类似的东西
var fetchModule = require("fetch");
fetchModule.fetch("http://202.120.227.11/")
.then(function(resp){
console.log(JSON.stringify(resp));
return resp;
})
.catch(function(err){
console.log(JSON.stringify(err));
return err;
});
Run Code Online (Sandbox Code Playgroud)
但then块永远不会被执行。有时catch块被执行并给出网络错误。但无论哪种情况,根据我的 tcpdump 记录,请求被发送并顺利收到响应。
因此,似乎 nativescript 出于某种原因过滤了响应。
有人经历过吗?
我正在使用ReactJS和create-react-app构建一个简单的 Web 应用程序。
我在 Heroku 上设置了一个后端 API,我可以在其中发出POST请求。一切正常,除了:
当我使用 fetch API 发出 POST 请求时,响应是 100% 正确的,但它只给了我 2 个标准标头。我想获得我的自定义标题。我在响应中添加了公开标头,这是情节扭曲:当我从 Chrome 检查工具或邮递员(API 工具)查看标头时,它会显示所有标头,包括我的自定义标头。这是我正在使用的提取代码 -
fetch(theUrl, {
method: 'POST',
body: JSON.stringify({
"placeholder": "placeholder"
})
})
.then(function(res) {
console.log(res.headers.get('CUSTOM_HEADER_NAME'));
})
Run Code Online (Sandbox Code Playgroud)
如果有什么不同,这个 fetch 方法是从 ReactJS 组件主体之外的函数调用的。
自定义标题的名称Image-Identification-Path,并在我的响应头报头Access-Control-Expose-Headers的Image-Identification-Path。
摘要:如何使用 fetch 获取自定义标头?
我正在使用此处指定的本机提取库。似乎每当返回 200 OK 以外的响应时,它都会引发字符串 response 异常Uncaught (in promise) TypeError: Failed to fetch。
有没有办法在特定的 HTTP 响应代码上捕获和分支并仍然查看响应数据?例如 401 响应?
我附上了我用作获取包装器的请求代码。
static request(url, data) {
let headers = {
"Authorization": window.localStorage.getItem("Authorization"),
"Content-Type": "application/json"
};
let options = {
method: "GET",
headers: headers,
mode: "no-cors",
cache: "no-cache",
};
if (data) {
options = {
method: "POST",
headers: headers,
mode: "no-cors",
cache: "no-cache",
body: JSON.stringify(data)
}
}
return new Promise(async (resolve, reject) => {
try {
let response = await fetch(url, …Run Code Online (Sandbox Code Playgroud) 我现在正在处理我的申请。我正在尝试将 fetch 用于登录页面,但即使阅读一些代码示例,我也不太了解如何使用 fetch。任何人都可以帮我得到吗?
例如,我必须使用这些信息登录到我的服务器。用户名:“用户” 密码:“1234”
然后我希望服务器返回登录成功与否,如果登录成功则返回一个令牌
我尝试使用此代码
render() {
return (
fetch('mysite', {
method: 'POST',
body: JSON.stringify({
username: "user",
password: "1234",
})
})
.then((response) => response.json())
.then((responseJson) => { return responseJson.success; })
.catch((error) => {
console.error(error); })
);
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何返回成功和令牌信息。是否有此提取的标题?其实我不知道如何放置这段代码。使用它来回报渲染部分是真的吗?
谢谢推荐。
我正在努力通过 CORS 获取授权:
const token = 'this.is.secret!';
fetch('http://corsserver/api/hello', {
method: 'get',
credentials: 'include',
mode: 'cors',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(response => {
console.log(response);
}).catch(error => {
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
当我运行此请求时,Chrome 将标头设置为:
Request Method:OPTIONS
我查了一下,这是一个预检请求。
我的意思是,这真的很酷。但是,我不知道如何在预检回来后发送实际请求,好吧!下一步是什么?如何发送 GET 请求?
我在这里遗漏了一些非常基本的东西。
我有一个工作图像上传前端/后端代码工作。现在我希望能够在上传后从服务器获取图像。
问题是图像必须在经过身份验证的路由后面,用户必须在标题或正文中传递 jwt 令牌。
当我尝试像这样获取图像时:
fetch(imageURL, {
method: 'GET',
headers: {
'x-access-token': localStorage.getItem('token')
}
Run Code Online (Sandbox Code Playgroud)
我只是得到一个 Form 对象作为响应:
<img alt="Your pic" src="[object FormData]">
Run Code Online (Sandbox Code Playgroud)
除了将 url 粘贴到 'src' 属性中之外,还有什么方法可以将图像放入 HTML 'img' 标签中,因为它会导致 401 (Unauthorized)
我目前正在学习如何使用ES8的fetch,async等待我目前有这个代码可以工作:
const url = "https://api.icndb.com/jokes/random";
async function tellJoke() {
let data = await (await fetch(url)).json();
return data.value.joke;
}
tellJoke().then(data => console.log(data));
Run Code Online (Sandbox Code Playgroud)
安慰:
"Chuck Norris can dereference NULL."
Run Code Online (Sandbox Code Playgroud)
但是我发现了一个使用箭头功能的片段,问题是我不知道如何在我当前的例子中按照我的方式返回我的值.
片段:
const fetchAsync = async () =>
await (await fetch(url)).json()
Run Code Online (Sandbox Code Playgroud)
如果这不是最佳做法让我知道,欢迎任何进一步阅读.
javascript async-await arrow-functions fetch-api ecmascript-2017
我刚刚学习 react 并创建了一个图库应用程序,但是我在将图片发布到 API 时遇到了问题。问题是,当我点击按钮时ADD,console.log 中什么也没有发生,我得到一个error 500.
这是我的带有 post 请求的组件:
class AddPhoto extends Component {
constructor(props) {
super(props);
this.state = {
modal: false,
images: [],
isLoading: false,
error: null,
};
this.toggle = this.toggle.bind(this);
this.handleClick = this.handleClick.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
toggle() {
this.setState({
modal: !this.state.modal
});
}
handleClick(event) {
event.preventDefault();
this.setState({
modal: !this.state.modal
});
}
handleSubmit(event){
event.preventDefault();
this.setState({ isLoading: true });
let path = this.props.path;
fetch(`http://.../gallery/${path}`, {
method: 'POST',
headers: {'Content-Type':'multipart/form-data'},
body: new FormData(document.getElementById('addPhoto'))
})
.then((response) …Run Code Online (Sandbox Code Playgroud) 使用本地Fetch API而不是HttpClient在Angular中有任何特定的技术缺陷吗?我是Angular的新手,不确定以这种方式“规避”所提供的接口是否安全。(例如,document根据文档,似乎不建议通过访问直接修改DOM 。)
我正在使用Angular 6,并且我不关心不支持的客户端fetch。
在试图理解返回结果时,我最终得到了这个简单的事情:
fetch('http://localhost:8081/position', {mode: 'cors'})
.then(response => {return response.json()})
.then(result => console.log(result));
Run Code Online (Sandbox Code Playgroud)
哪个有效 - 它打印响应的json.
但这不起作用:
fetch('http://localhost:8081/position', {mode: 'cors'})
.then(response => {console.log(response.json()); return response.json();})
.then(result => console.log(result));
Run Code Online (Sandbox Code Playgroud)
它发了 Uncaught (in promise) TypeError: Failed to execute 'json' on 'Response': body stream is locked
这是为什么?
fetch-api ×10
javascript ×7
reactjs ×3
cors ×2
angular ×1
async-await ×1
fetch ×1
http ×1
http-headers ×1
jwt ×1
login ×1
nativescript ×1
node.js ×1
post ×1
preflight ×1
promise ×1
react-native ×1
react-redux ×1