use*_*761 10 javascript curl parse-platform react-native
我可以使用以下命令从解析中成功检索对象:
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`
Run Code Online (Sandbox Code Playgroud)
但我正在努力将其转换为javascript,使用下面的代码我会得到状态为的响应200.我假设错误是将data-urlencode转换为数据:
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)
})
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
kig*_*iri 22
由于我经常想这样做,我制作了https://kigiri.github.io/fetch/,你可以复制/粘贴一个curl命令,它会给你提取代码.
如果要以不同方式使用它,可以签出源.
好吧基本上你需要另一个.所以它与承诺有关.我在阅读了这篇很棒的文章http://blog.gospodarets.com/fetch_in_action/后想出来了
你需要做的就是这个
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
})
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白你为什么要返回response.text().它还有一些可以做的承诺
https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise
因为当我从第一个访问response.text()时.它返回一个承诺.
希望其他一些绅士可以评论和解释为什么返回response.text()将承诺转换为您需要的数据.
- 编辑 -
我添加了一些返回来显示如何让getObject将响应数据作为对象返回.所以
var d = getObject();
Run Code Online (Sandbox Code Playgroud)
现在将返回响应作为js对象.
| 归档时间: |
|
| 查看次数: |
16008 次 |
| 最近记录: |