gpt4 book ai didi

javascript - Parse Cloud httpRequest 不返回响应

转载 作者:行者123 更新时间:2023-12-02 16:04:37 26 4
gpt4 key购买 nike

我正在从浏览器中的 js 调用我的 Parse Cloud 函数。我这样做只是为了测试。我正在尝试对 http://api.geonames.org 进行 GET 调用。如果我在浏览器中执行它 http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo 它确实有效。它返回:

{"geonames":[{"fcodeName":"capital of a political entity","toponymName":"Mexico City","countrycode":"MX","fcl":"P","fclName":"city, village,...","name":"Mexiko-Stadt","wikipedia":"en.wikipedia.org/wiki/Mexico_City","lng":-99.12766456604,"fcode":"PPLC","geonameId":3530597,"lat":19.428472427036,"population":12294193},{"fcodeName":"capital of a political entity","toponymName":"Beijing","countrycode":"CN","fcl":"P","fclName":"city, village,...","name":"Peking","wikipedia":"en.wikipedia.org/wiki/Beijing","lng":116.397228240967,"fcode":"PPLC","geonameId":1816670,"lat":39.9074977414405,"population":11716620},{"fcodeName":"capital of a political entity","toponymName":"Manila","countrycode":"PH","fcl":"P","fclName":"city, village,...","name":"Manila","wikipedia":"en.wikipedia.org/wiki/Manila","lng":120.9822,"fcode":"PPLC","geonameId":1701668,"lat":14.6042,"population":10444527},{"fcodeName":"capital of a political entity","toponymName":"Dhaka","countrycode":"BD","fcl":"P","fclName":"city, village,...","name":"Dhaka","wikipedia":"en.wikipedia.org/wiki/Dhaka","lng":90.40743827819824,"fcode":"PPLC","geonameId":1185241,"lat":23.710395616597037,"population":10356500},{"fcodeName":"capital of a political entity","toponymName":"Seoul","countrycode":"KR","fcl":"P","fclName":"city, village,...","name":"Seoul","wikipedia":"en.wikipedia.org/wiki/Seoul","lng":126.9784,"fcode":"PPLC","geonameId":1835848,"lat":37.566,"population":10349312},{"fcodeName":"capital of a political entity","toponymName":"Jakarta","countrycode":"ID","fcl":"P","fclName":"city, village,...","name":"Jakarta","wikipedia":"en.wikipedia.org/wiki/Jakarta","lng":106.84513092041016,"fcode":"PPLC","geonameId":1642911,"lat":-6.214623197035775,"population":8540121},{"fcodeName":"capital of a political entity","toponymName":"Tokyo","countrycode":"JP","fcl":"P","fclName":"city, village,...","name":"Tokio","wikipedia":"de.wikipedia.org/wiki/Tokyo","lng":139.69171,"fcode":"PPLC","geonameId":1850147,"lat":35.6895,"population":8336599},{"fcodeName":"capital of a political entity","toponymName":"Taipei","countrycode":"TW","fcl":"P","fclName":"city, village,...","name":"Taipeh","wikipedia":"de.wikipedia.org/wiki/Taipei","lng":121.531846,"fcode":"PPLC","geonameId":1668341,"lat":25.047763,"population":7871900},{"fcodeName":"capital of a political entity","toponymName":"Bogotá","countrycode":"CO","fcl":"P","fclName":"city, village,...","name":"Bogotá","wikipedia":"en.wikipedia.org/wiki/Bogot%C3%A1","lng":-74.08175468444824,"fcode":"PPLC","geonameId":3688689,"lat":4.609705849789108,"population":7674366},{"fcodeName":"capital of a political entity","toponymName":"Hong Kong","countrycode":"HK","fcl":"P","fclName":"city, village,...","name":"Hong Kong","wikipedia":"en.wikipedia.org/wiki/Hong_Kong","lng":114.157691001892,"fcode":"PPLC","geonameId":1819729,"lat":22.2855225817732,"population":7012738}]}

如果我在我的云代码中执行它:

Parse.Cloud.define("testRequest", function(request , response){
Parse.Cloud.httpRequest({
url: 'http://api.geonames.org/citiesJSON',
params: {
'north' : '44.1',
'south' : '-9.9',
'east' : '-22.4',
'west' : '55.2',
'lang' : 'de',
'username' : 'demo'
},
success:function(httpResponse){
console.log(httpResponse.text);
response.success("success from response.success>>>>");
console.log('httpResponse->'+ httpResponse.response +' <<<');
console.log('data>>'+httpResponse.data);
},
error:function(httpResponse){
console.error('Request failed with response code ' + httpResponse.status);
response.error(httpResponse.status);
},
});
});

在我的本地计算机中使用以下脚本:

<script type="text/javascript">
Parse.initialize("KEY", "KEY");



Parse.Cloud.run('testRequest', {}, {
success: function(result) {
alert(result.httpRequest+"<----RESULT!")
// result is 'Hello world!'
},
error: function(error) {
alert("Something went wrong"+error.message);
}
});
</script>

它返回 200 状态,但没有数据。我确保我部署了我的代码,并且我在 Parse.com 的“云代码”选项卡中看到了最新代码,因此绝对不是我在云上运行旧代码的情况。

您知道如何正确地从 Cloud Code 进行成功的 GET 调用吗?非常感谢有关此主题的任何帮助或帮助我纠正代码中的任何错误!

最佳答案

您应该通过以下方式从 CloudCode 返回数据

response.success(httpResponse.text);

并像这样在 JavaScript 中使用数据

Parse.Cloud.run('testRequest', {}, {
success: function(result) {
alert(result);
}
});

您现在就会看到结果。

关于javascript - Parse Cloud httpRequest 不返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30878292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com