从Worklight获得HTTP适配器的响应 [英] Getting response from HTTP Adapter from Worklight

查看:131
本文介绍了从Worklight获得HTTP适配器的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Worklight的新手.我试图从http适配器获取json响应, 但我无法将其显示在设备上. 我在JavaScript代码中添加了一些警报,发现它返回的json对象的大小为未定义".

I am new to worklight. I tried to get the json response from the http adapter, but I am unable to get it to display on device. I added some alerts in my javascript code and found that it is returning the size of json object as `undefined``.

这是我的适配器javascript文件:

Here is my adapter javascript file:

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文件:

My main.js file:

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 : loadHTTPQuerySuccess,
        onFailure : loadHTTPQueryFailure
    });

}

function loadHTTPQuerySuccess(result){

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

}

function loadHTTPQueryFailure(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);
        }
    }
}

请提出我在做错什么.

我的示例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"
  },

推荐答案

如果我正确地理解了您的问题,请通过items.size尝试获取结果的长度.如果要获取结果的长度,则应使用items.invocationResult.results.length,它会为您提供结果的总数,其中items是来自适配器的响应,而invocationResult包含结果和其他参数,您将获得这些结果和其他参数访问结果以仅访问特定输出.

If i understood you question properly, through items.size you are trying to get the length of results. If you want to get the length of results you should use items.invocationResult.results.length which will give you the total number of results, where items is the response coming from the adapter and invocationResult contains the results and other paramaters, from which you will have to access the results for accessing only the particular output.

如果我不能正确理解您的问题,请准确告诉我您要通过items.size进行哪些尝试

If i didn't understand your question properly, please tell me exactly what you are trying to get through items.size

这篇关于从Worklight获得HTTP适配器的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆