2014-02-10 2 views
1

새로운 기능입니다. http 어댑터, 에서 json 응답을 얻으려고했지만 장치에 표시 할 수 없습니다. javascript 코드에 몇 가지 알림을 추가하고 json 개체의 크기를`undefined`로 반환한다는 것을 알았습니다. Worklight에서 HTTP 어댑터의 응답 받기

여기 내 어댑터입니다 자바 스크립트 파일 :

function getGooglePlaces(location,name) { 

    var input = { 
     method : 'get', 
     returnedContentType : 'json', 
     path : 'maps/api/place/search/json', 
     headers: { 
      Host: 'maps.googleapis.com' 
     }, 
     parameters : { 
      'key'  : MyKey, 
      'location' : location, 
      'radius' : '10000', 
      'sensor' : 'false', 
      'name'  : name 
     } 
    }; 

    var response = WL.Server.invokeHttp(input); 

    return response; 

} 

function addGooglePlace(param1) { 

    var input = { 
     method : 'put', 
     returnedContentType : 'json', 
     path : 'userInputRequired' 
    }; 

    return WL.Server.invokeHttp(input); 
} 

내 main.js 파일 :

function getLocation() 
{ 

    if (navigator.geolocation) 
    { 
     navigator.geolocation.getCurrentPosition(loadHTTPRecords); 
    } 
    else{x.innerHTML="Geolocation is not supported by this browser.";} 
} 

function loadHTTPRecords(position){ 

    var invocationData = { 
     adapter : 'GooglePlaces', 
     procedure : 'getGooglePlaces', 
     parameters : [position.coords.latitude+','+position.coords.longitude,'dead battery'] 
    }; 

    WL.Client.invokeProcedure(invocationData,{ 
     onSuccess : loadHTTPQueerySuccess, 
     onFailure : loadHTTPQueeryFailure 
    }); 

} 

function loadHTTPQueerySuccess(result){ 

    WL.Logger.debug("Retrieve success" + JSON.stringify(result)); 
    displayFeeds(result); 

} 

function loadHTTPQueeryFailure(result){ 

    WL.Logger.error("Retrieve failure"); 
} 

function displayFeeds(items){ 
    alert("In displayFeeds"); 

    // Get the size of an object 

    var ul = $('#itemsList'); 
    alert("before for loop"); 

    alert(items.size); 
    for (var i = 0; i < items.size; i++) { 
     alert("inside for loop 1"); 
     for(var j=0;j<i;j++){ 
      alert("in for loop 2"); 
      var li = $('<li/>').html(items[i].name); 

      li.append($('<hr>')); 
      ul.append(li); 
     } 
    } 
} 

내가 뭘 잘못 제안하십시오. 난 당신이 당신은 결과의 길이를 얻기 위해 노력하고있다 items.size을 통해 제대로 질문을 이해하면

내 샘플 JSON 응답

{ 
"html_attributions": [ 
], 
    "isSuccessful": true, 
    "responseHeaders": { 
    "Alternate-Protocol": "443:quic", 
    "Cache-Control": "public, max-age=300", 
    "Content-Type": "application\/json; charset=UTF-8", 
    "Date": "Tue, 11 Feb 2014 12:04:13 GMT", 
    "Expires": "Tue, 11 Feb 2014 12:09:13 GMT", 
    "Server": "mafe", 
    "Transfer-Encoding": "chunked", 
    "Vary": "Accept-Language", 
    "X-Frame-Options": "SAMEORIGIN", 
    "X-XSS-Protection": "1; mode=block" 
}, 
    "responseTime": 236, 
    "results": [ 
     { 
     "geometry": { 
     "location": { 
      "lat": 52.057049, 
      "lng": 1.153298 
     } 
    }, 
    "icon": "http:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/cafe-71.png", 
    "id": "ec0955fb06fd95d639c89d12475624627250abac", 
    "name": "Costa Coffee", 
    "opening_hours": { 
     "open_now": true 
    }, 
    "price_level": 2, 
    "rating": 3.9, 
    "reference": "CnRuAAAABmdY6kIxRQZw68hqjZ_wwBE29sdSgYuOkXf2IvZTe77aG-AgoCaahu1c9cddHA0Z1D2EdELAEuDyl38xV1G5YcvP3pOm2p0IwVkuvYIJSA1IKAGLIQym21SpXvhUSqBxrpHKBvgTNnUg69lHROaMyxIQvvP8SeCG_dzKi_JgrdrgRRoUQXqH4UkDtA-58bCbdRzUCdXTRVU", 
    "types": [ 
     "cafe", 
     "food", 
     "establishment" 
    ], 
    "vicinity": "1-5 Queen St, Ipswich" 
    }, 
+0

사용하여 어댑터 절차를 호출하는 동안 응답을받을 수 있나요 ** 호출 작업등 절차 ** –

+0

그래, 내가 프로 시저를 호출 할 때 응답을 가지고 있지만 배열 결과에 액세스하려고하면 문제가 발생한다. 'undefined'가된다. – ravi

+0

json을 잘못된 방향으로 가로 지르고 있다고 생각하기 때문에 질문에 샘플 결과를 제공 할 수 있는가? –

답변

2

. 결과의 길이를 얻으려면 items.invocationResult.results.length을 사용해야합니다. 결과는 어댑터에서 오는 응답이며, invocationResult에는 결과 및 다른 매개 변수가 포함되어있어 결과에 액세스해야합니다 특정 출력에만 액세스 할 수 있습니다.

내가 제대로 질문을 이해하지 않은 경우, 당신은 items.size을 통해 얻을려고 정확히 말해주십시오

+0

질문이 있습니다. JSON.stringify를 사용하여 json 객체를 문자열로 변환 한 다음 items.invocationResult.results를 사용해야합니다. – ravi

+0

예. 사용 가능합니다. 두 가지 모두 동일한 결과를 제공하지만 결과 길이에 액세스하려는 경우 item.invocationResult.results를 직접 사용할 수 있습니다. – sasi

+0

감사합니다 sasi, 나는 그것을 하나의 doubt.in 같은 응답을 나는 각 결과의 price_level에 액세스하고 싶지만, 내가 그것을 실행할 때 그것은 정의되지 않는다. – ravi