我想通过 fetch API 将一些数据发送到服务器,以便它可以基于此查询数据库。与 Ajax 不同,我找不到任何合适的方法来做到这一点。这是我的 fetch 电话:
{fetch("http://192.168.90.53:4000/game/?email=adicool2294@gmail.com", {method: "GET"} )
.then((response) => response.json())
.then((responseData) => {
id= responseData.id;
name= responseData.name;
console.log(responseData);
})
.catch((error) => {
console.warn(error);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我想发送一个参数,例如电子邮件,以便服务器可以使用它查询数据库。我正在使用 react-native android 因此不能使用 Ajax 请求。我只想使用 GET 方法。目前,服务器未在 req.query 中显示我在 URL 中传递的查询参数
我正在使用javascript fetch API使用以下代码查询跨域api:
fetch('https://api.com/search?query="2016"').then(function (response) {
console.log(response.headers.get('Access-Control-Allow-Headers'))
console.log(response.headers.get('Content-Range'))
console.log(response.headers.get('Accept-Range'))
console.log(response.headers.get('Date'))
console.log(response.headers.get('Content-Type'))
})
Run Code Online (Sandbox Code Playgroud)
响应标头如下:
Accept-Range:cars 300
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Content-Range, Accept-Range
Access-Control-Allow-Methods:PUT, POST, OPTIONS, GET
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:keep-alive
Content-Range:0-99/1941
Content-Type:application/json
Date:Tue, 12 Jan 2016 12:17:55 GMT
Transfer-Encoding:chunked
Run Code Online (Sandbox Code Playgroud)
非常奇怪的是,只有"Content-Type"适用于其他我得到null:
null
null
null
null
application/json
Run Code Online (Sandbox Code Playgroud)
我需要做什么来检索那些空标题?
我有一个 ASP.NET MVC 5 应用程序,它处理来自我的网站的使用 fetch API 发出的请求。当我的控制器中发生异常时,我使用自定义的“OnException”方法(从 System.Web.Mvc.Controller 重写)以 JSON 格式发送结果,其中包含有关错误的数据。我这样做是因为我不想发送默认的 HTML 错误页面。在这个对所有控制器都是全局的方法中,我需要检查请求是否已使用 fetch API 执行(如果没有,我什么都不做)。
我怎样才能做到这一点 ?
在客户端浏览器中使用 fetch api,我没有遇到 GET 或 POST 问题,但是我遇到了 fetch 和 DELETE 问题。它似乎将 DELETE 请求方法更改为 OPTIONS。
大多数研究表明是 cors 问题,但对我来说,我已经涵盖了 cors 问题。
我不确定这是否是有效的 fetch spec api 链接,但它显示:
上市方法的CORS安全是一个方法
GET,HEAD或POST。
我不确定这是否意味着我不能在使用 cors 获取时使用 DELETE,这就是我遇到问题的原因?
浏览器代码:
var request =
new Request(url, {
credentials: 'include',
mode: 'cors',
method: 'DELETE'
});
return fetch(request)
.then(this.fetchError.bind(this))
.then(this.json)
.then((response)=> {
this.set(`uploadState.${index}.value`, false);
})
.catch((e) => {
console.log(e);
});
},
Run Code Online (Sandbox Code Playgroud)
chrome 网络标签:
Request URL:http://72.12.4.3:9000/api/v1/listings/3/47/image-3-47-1492565415145.jpeg
Request Method:OPTIONS
Status Code:401 Unauthorized
Remote Address:72.12.4.3:9000
Response Headers
view …Run Code Online (Sandbox Code Playgroud) 这就是我的代码的样子
let body = {
authCode: "XXXX",
clientId: "YYYYYY",
clientSecret: "ZZZZZZ"
};
fetch('https://api.myapp.com/oauth/token',{
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
mode: 'no-cors',
body: body
}).then(function(response){
console.log("response: ", response);
}).catch(function(error){
console.log("could not get tokens: ", error);
})
Run Code Online (Sandbox Code Playgroud)
我试着这样做curl command,这就是它的样子
? ~ curl -X POST -H "Content-Type: application/json" -d '{
"authCode": "XXXX",
"clientId": "YYYYY",
"clientSecret": "ZZZZZZZZ"
}' https://api.myapp.com/oauth/token
{"authToken":"e141kjhkwr3432ca9b3d2128385ad91db4cd2:cca6b151-cab4-4de2-81db-9a739a62ae88:23000000"}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
更新
改成follow后,结果还是 HTTP 415
fetch('https://api.myapp.com/oauth/token',{
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
mode: …Run Code Online (Sandbox Code Playgroud) 我的浏览器在 devtools 控制台中记录了以下消息:
请求的资源上不存在“Access-Control-Allow-Origin”标头……响应的 HTTP 状态代码为 503。
背景:我有两个应用程序。一个是连接到 Mongo 数据库的 Express Node 应用程序。另一个是基本的 Web 应用程序,它POST通过 Fetch API 向 Node 应用程序发出请求,以从 Mongo 获取数据。
问题:虽然我CORS在本地机器上没有收到任何错误,但一旦我将基本 Web 应用程序部署到生产环境,就会出现以下错误。POST向 Node 应用程序发出请求并给我以下信息的 Web 应用程序:
该POST请求似乎确实有效,并且数据已保存到 Mongo 中,但此错误在 Heroku 中被标记为“严重错误”并且非常烦人。
我意识到我可以no-cors在 Fetch 中设置该选项,但我相信它是必需的,因为我正在向与原点不同的 url 发出请求。对?
Express 节点应用程序代码
在我的app.js文件中,我设置了正确的标头以确保其他应用程序可以从不同来源发出请求
应用程序.js
// Add headers so we can make API requests
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); …Run Code Online (Sandbox Code Playgroud) 与 API 交互时,自定义 Header 键始终变为小写。我使用Fetch, Axios,XMLHttpRequest和Frisbee(javascript 网络库) 但键总是小写
我这样的代码片段(使用fetch()方法),我推送的关键是:'Token-Api'但服务器收到:'token-api',所以它显示错误 401。它与 Postman 一起工作:
const request = 'https://abcxyz';
fetch(request, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Token-Api':'...abcxyz....'
}
}).then((response) => {
log(abcxyz)
})...
Run Code Online (Sandbox Code Playgroud)
我需要做什么?
我开始构建一个 Reactjs 应用程序,它使用 Fetch API 来处理我对 Web API 的请求。在为 Azure 应用程序设置 Microsoft 身份验证之前,我没有遇到任何问题,现在我的请求引发了 CORS 错误。
为了解决可能出现的问题,我使用 XHR 设置了相同的请求。我能够毫无问题地完成请求。我想知道是否有人能够告诉我我哪里出错了?
XHR版
var oReq = new XMLHttpRequest();
oReq.open('get', 'https://myazureapp.azurewebsites.net/api/user', true);
oReq.send();
Run Code Online (Sandbox Code Playgroud)
获取 API 版本
fetch('https://myazureapp.azurewebsites.net/api/user');
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'https://myazureapp.azurewebsites.net' is therefore
not allowed access. If an opaque response serves your needs, set the
request's mode to 'no-cors' to fetch the resource with CORS disabled.
Run Code Online (Sandbox Code Playgroud)
任何关于我可能做错了什么的见解都会很棒。如果您需要更多信息,请告诉我,我会相应地更新我的问题。
我有一个生成的 React 站点,我托管在 S3 存储桶中。我的一个组件在加载时尝试获取一些东西:
require('isomorphic-fetch')
...
componentDidMount() {
fetch(`${url}`)
.then(res => {
console.log(res);
this.setState({
users: res
})
})
.catch(e => {
// do nothing
})
}
Run Code Online (Sandbox Code Playgroud)
在url我取是AWS API网关。我通过下拉菜单在那里启用了 CORS,对默认配置没有任何更改。
在我的控制台中,对于远程站点和开发过程中的本地站点,我看到:
“加载失败url:请求的资源上不存在‘Access-Control-Allow-Origin’标头。” 等等
但是,在 Chrome 网络选项卡中,我可以看到请求和响应,状态为 200 等。但是,在控制台中, myconsole.log和this.setState永远不会被调用。
我知道 CORS 是一个常见的痛点,很多问题都涉及到 CORS。我的问题:为什么响应在“网络”选项卡中没有显示错误,而同时在控制台中出现错误?
所以我使用RiveScript的JavaScript 端口,它使用 ajax,当然我不想再使用 jQuery。只有一行 ajax,我想将其更改为新的 Fetch API。
**FYI: You can see the ajax code in line 1795 of the CDN.**
Run Code Online (Sandbox Code Playgroud)
所以这是原始代码:
return $.ajax({
url: file,
dataType: "text",
success: (function(_this) {
return function(data, textStatus, xhr) {
_this.say("Loading file " + file + " complete.");
_this.parse(file, data, onError);
delete _this._pending[loadCount][file];
if (Object.keys(_this._pending[loadCount]).length === 0) {
if (typeof onSuccess === "function") {
return onSuccess.call(void 0, loadCount);
}
}
};
})(this),
error: (function(_this) {
return function(xhr, textStatus, errorThrown) {
_this.say("Ajax …Run Code Online (Sandbox Code Playgroud) fetch-api ×10
javascript ×8
cors ×4
fetch ×2
node.js ×2
react-native ×2
reactjs ×2
ajax ×1
axios ×1
curl ×1
http ×1
jquery ×1
json ×1
rivescript ×1