[英]How to make HTTPS requests with serverside javascript using Worklight?
我正忙着使用IBM worklight ,我正在嘗試創建一個適配器來從Google Places API提供一些數據。
我想打電話給這個網址:
https://maps.googleapis.com/maps/api/place/search/json?key=AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM&location=52.0700,1.1400&radius=10000&sensor=false&name=coffee
執行此URL在瀏覽器中工作正常,並顯示我試圖通過Worklight獲取的一些不錯的JSON。
Worklight適配器是用Javascript創建的,這是我到目前為止所擁有的:
function getCoffeeHouses() {
var input = {
method : 'get',
returnedContentType : 'json',
path : 'maps/api/place/search/json',
parameters : {
'key' : 'AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM',
'location' : '52.0700,1.1400',
'radius' : '10000',
'sensor' : 'false',
'name' : 'coffee'
}
};
var response = WL.Server.invokeHttp(input);
// Extract latitude and longitude from the response.
var type = typeof response;
if ("object" == type) {
if (true == response["isSuccessful"]) {
// Return JSON object with lat and lng.
return response["results"];
}
else {
// Returning null. Web request was not successful.
return null;
}
}
else {
// Returning null. Response is not an object.
return null;
}
}
當我測試上面的內容時,這是我進入控制台的結果:
Failed to parse JSON string
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
</style>
<a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/maps/api/place/search/json?key=AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM&location=52.0700%2C1.1400&radius=10000&sensor=false&name=coffee</code> was not found on this server. <ins>That’s all we know.</ins>
Caused by: java.io.IOException: Unexpected character '<' on line 1, column 1
[2012-07-23 11:08:57] An error occurred while invoking procedure CoffeeFinder/getCoffeeHouses parameters: {
"arr": [
]
}
null
Caused by: null
我認為,這可能是因為適配器請求為HTTP,而應該使用HTTPS。
如果我在瀏覽器中更改了使用HTTP的請求,則會顯示類似的結果。
問題:我可以通過更改上面的Javascript來發出HTTPS請求,還是我誤解了worklight適配器?
如果您未在請求中指定主機標頭,則看起來像googleapis將無效。 添加后,一切正常:
這是適配器的XML部分
<connectivity>
<connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
<protocol>https</protocol>
<domain>maps.googleapis.com</domain>
<port>443</port>
</connectionPolicy>
<loadConstraints maxConcurrentConnectionsPerNode="2" />
</connectivity>
這是適配器的JS:
function doGet() {
var input = {
method : 'get',
returnedContentType : 'json',
path : 'maps/api/place/search/json',
headers: {
Host: 'maps.googleapis.com'
},
parameters : {
'key' : 'AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM',
'location' : '52.0700,1.1400',
'radius' : '10000',
'sensor' : 'false',
'name' : 'coffee'
}
};
var response = WL.Server.invokeHttp(input);
return response;
}
在你的適配器中還有一個{ADAPTER NAME} .xml
在其中,在connectionPolicy下的連接下,有協議。 你有沒有把它改成https並部署?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.