如何使用Worklight使用服务器端javascript发出HTTPS请求
How to make HTTPS requests with serverside javascript using Worklight?
我正在摆弄IBM工作灯,并试图创建一个适配器来从Google places API提供一些数据。
我想调用这个URL:
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请求吗?还是我误解了工作灯适配器
如果您不在请求中指定Host标头,那么看起来Googleapi将不起作用。添加后,一切正常:
这是适配器的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并部署了吗?
相关文章:
- 如何通过类比curl请求从JavaScript发出请求
- 如何使用Worklight使用服务器端javascript发出HTTPS请求
- 使用 javascript 发出多个 HTTP GET 请求
- 使用 Javascript 发出警报
- 如何在 Vxml 中使用 JavaScript 发出 HTTP 请求
- 通过ASP.NET代码中的javascript发出警报后停止回发
- 如何从javascript发出ajax请求来实现php核心功能
- 用JavaScript发出POST请求并加载返回的页面(Sinatra)
- 如何使用Javascript发出警报
- 检查网站是否正在使用javascript发出X http请求
- 你能从Javascript发出HTTP PATCH请求吗?
- 跟踪404错误页面或如何监控浏览器使用Javascript发出的http头请求状态
- 通过使用javascript发出HTTP GET请求来显示响应(xml)
- 使用codeigniter和javascript发出声音通知
- 点击按钮后,点击如何使用javascript发出请求,并使用ajax MVC Kendo UI网格调用读取方法
- 如何使用 ajax 或 javascript 发出跨域请求
- 为什么我不能使用 JavaScript 发出 API 请求
- 3种用Javascript发出跨域请求的方法
- 使用JavaScript发出自动刷新的背景图像
- 如何用JavaScript发出get请求