将 curl GET 转换为 javascript 获取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31039629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert curl GET to javascript fetch
提问by user3430761
I am able to successfully retrieve an object from parse using command below:
我能够使用以下命令从解析中成功检索对象:
curl -X GET \
-H "X-Parse-Application-Id:1231231231" \
-H "X-Parse-REST-API-Key: 131231231" \
-G \
--data-urlencode 'where={"uid":"12312312"}' \
https://api.parse.com/1/classes/birthday`
But I'm struggling converting it to javascript, using the code below I will get response with a statusof 200
. I assume error is converting data-urlencodeto data:
但是我正在努力将它转换为 javascript,使用下面的代码我会得到状态为200
. 我假设错误是将data-urlencode转换为data:
var getObject = function {
var url = "https://api.parse.com";
url += '/1/classes/birthday';
fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
'X-Parse-Application-Id': '12122',
'X-Parse-REST-API-Key': '12121',
'Content-Type': 'application/json',
},
data: '{"where":{"uid":"12312312"}}'
})
.then(function(request, results) {
console.log(request)
console.log(results)
})
}
Thanks!
谢谢!
回答by kigiri
Since I often wanted to do this, I made https://kigiri.github.io/fetch/, you can copy / paste a curl command it will give you the fetch code.
由于我经常想这样做,我制作了https://kigiri.github.io/fetch/,您可以复制/粘贴 curl 命令,它会给您获取代码。
You can checkout the source if you want to use it differently.
如果你想以不同的方式使用它,你可以签出源。
edit:The feature was added to chrome devtools
Thanks chrome dev team
回答by Charles Haro
Ok so basically you need another then. So it has something to do with promises. I figured it out after reading this awesome article http://blog.gospodarets.com/fetch_in_action/
好的,所以基本上你需要另一个。所以它与承诺有关。在阅读了这篇很棒的文章http://blog.gospodarets.com/fetch_in_action/后,我想通了
What you need to do is this
你需要做的是这个
var getObject = function {
var url = "https://api.parse.com";
url += '/1/classes/birthday';
return fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
'X-Parse-Application-Id': '12122',
'X-Parse-REST-API-Key': '12121',
'Content-Type': 'application/json',
},
data: '{"where":{"uid":"12312312"}}'
})
.then(function(response) {
return response.text();
})
.then(function(data){
console.log(data); //this will just be text
var data_obj = JSON.parse(data);
return data_obj
})
}
I don't really understand why you have to return response.text(). Again it has something to do promises
我真的不明白为什么你必须返回 response.text()。再一次,它有一些事情要做承诺
https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise
https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise
Because when I access response.text() from the first then. It returns a promise.
因为当我从第一个访问 response.text() 时。它返回一个承诺。
Hopefully some other kind gentleman can comment and explain why returning response.text() turns the promise into the data you need.
希望其他善良的绅士可以评论并解释为什么返回 response.text() 将承诺转换为您需要的数据。
--EDIT--
- 编辑 -
I added some returns to show how you can have getObject return out the response data as an object. So
我添加了一些返回来展示如何让 getObject 将响应数据作为对象返回。所以
var d = getObject();
will return the response as a js object now.
现在将响应作为 js 对象返回。