2015-06-16 47 views
0

我在瀏覽器中從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執行它,它確實有效。它返回:解析雲端httpRequest未返回響應

{"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選項卡中看到了最新的代碼,因此我確實不會在雲上運行舊代碼。

您是否知道如何從Cloud Code中正確進行GET調用? 關於這個主題的任何幫助或幫助我糾正我的代碼中的任何錯誤,非常感謝!

+0

如果它正是你正在運行的代碼,CloudCode函數返回一個字符串'response.success >>>>'成功,對不對?但是你的JavaScript代碼試圖從字符串中獲得'httpRequest'。 – iForests

+0

你是什麼意思?對不起,我聽不懂......你是說我不應該從response.success返回'成功>>>>'? – idelara

+0

是的。我認爲你應該'response.success(httpResponse.text);'和'alert(result);'。瀏覽器會通知Json'{「geonames」:[{「fcodeName」:「ca ...'now。 – iForests

回答

2

您應該CloudCode通過

response.success(httpResponse.text); 

返回的數據,並使用這些數據在JavaScript這樣

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

現在,您將看到結果。

+0

謝謝你的幫助! – idelara

相關問題