2012-07-23 29 views
8

Tôi đang đùa giỡn với IBM worklight và đang cố tạo bộ điều hợp để cấp một số dữ liệu từ Google places API.Làm thế nào để thực hiện các yêu cầu HTTPS với javascript serveride bằng Worklight?

Tôi muốn gọi URL này:

https://maps.googleapis.com/maps/api/place/search/json?key=AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM&location=52.0700,1.1400&radius=10000&sensor=false&name=coffee 

Thi URL này hoạt động tốt trong một trình duyệt, và hiển thị một số JSON đẹp mà tôi đang cố gắng để có được thông qua Worklight.

Các Worklight adapter được tạo ra trong Javascript, đây là những gì tôi có cho đến nay:

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; 
    } 
} 

Và đây là kết quả mà tôi nhận được trong giao diện điều khiển, khi tôi kiểm tra trên:

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&amp;location=52.0700%2C1.1400&amp;radius=10000&amp;sensor=false&amp;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 

Tôi nghĩ, điều này có thể gây ra do bộ điều hợp đang yêu cầu dưới dạng HTTP, trong khi bộ điều hợp nên sử dụng HTTPS.

Nếu tôi thay đổi yêu cầu sử dụng HTTP trong trình duyệt, nó sẽ hiển thị kết quả tương tự.

Câu hỏi: Tôi có thể thực hiện yêu cầu HTTPS bằng cách thay đổi Javascript ở trên hoặc tôi có phải là bộ điều hợp công việc hiểu lầm không?

Trả lời

7

Có vẻ như googleapis sẽ không hoạt động nếu bạn không chỉ định tiêu đề Máy chủ bên trong yêu cầu của mình. Sau khi thêm nó mọi thứ hoạt động như mong muốn:

Đây là phần XML adapter của

<connectivity> 
    <connectionPolicy xsi:type="http:HTTPConnectionPolicyType"> 
     <protocol>https</protocol> 
     <domain>maps.googleapis.com</domain> 
     <port>443</port>    
    </connectionPolicy> 
    <loadConstraints maxConcurrentConnectionsPerNode="2" /> 
</connectivity> 

Đây là JS adapter:

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; 

}

+0

tôi cũng có một vấn đề tương tự .. tôi đã đề cập đến hầu hết tất cả các loại nội dung nhưng không hoạt động chút nào. – Sarath

+0

Mô tả lỗi là "Máy chủ từ chối yêu cầu này vì thực thể yêu cầu nằm trong một định dạng không được hỗ trợ bởi tài nguyên được yêu cầu cho phương thức được yêu cầu (Loại phương tiện không được hỗ trợ) ".... tôi đã chỉ định Content-Type: 'application/json' .. nhưng vẫn là vấn đề presist .. – Sarath

2

trong adapter của bạn đó cũng là một {ADAPTER NAME} .xml

Trong đó, dưới khả năng kết nối dưới connectionPolicy, có giao thức. bạn có thay đổi nó thành https và triển khai không?

Các vấn đề liên quan