2016-07-15 46 views
0

我正在嘗試使用JavaScript HTTP適配器,以便從Bluemix中的Cloudant BD獲取一些數據。爲此,我使用TypeScript使用MFPF8和Ionic2。在JS HTTP適配器中爲MobileFirst添加參數8

因爲我需要從我以前不知道它的名稱的數據庫中獲取特定文檔,所以我需要一個HTTP適配器,它允許我將文件名作爲參數發送。

我有以下適配器實現文件:

function getMenus() { 
    var input = { 
     method : 'get', 
     returnedContentType : 'json', 
     path : 'menus/_all_docs?descending=true' 
    }; 

    return MFP.Server.invokeHttp(input); 
} 

function getSpecificMenu(menuName) { 

    var input = { 
     method : 'get', 
     returnedContentType : 'json', 
     path : 'menus/'+menuName 
    }; 

    return MFP.Server.invokeHTTP(input); 

} 

這裏是adapter.xml

<mfp:adapter name="menus" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:mfp="http://www.ibm.com/mfp/integration" 
      xmlns:http="http://www.ibm.com/mfp/integration/http"> 

    <displayName>menus</displayName> 
    <description>menus</description> 
    <connectivity> 
     <connectionPolicy xsi:type="http:HTTPConnectionPolicyType"> 
      <protocol>https</protocol> 
      <domain>bluemixcloudanthost.com</domain> 
      <port>443</port> 
      <connectionTimeoutInMilliseconds>30000</connectionTimeoutInMilliseconds> 
      <socketTimeoutInMilliseconds>30000</socketTimeoutInMilliseconds> 
      <maxConcurrentConnectionsPerNode>50</maxConcurrentConnectionsPerNode> 

      <authentication> 
       <basic/> 
       <serverIdentity> 
        <username>user</username> 
        <password>pass</password> 
       </serverIdentity> 
      </authentication> 

     </connectionPolicy> 
    </connectivity> 

    <procedure name="getMenus" secured="false"/> 
    <procedure name="getSpecificMenu" secured="false"/> 

</mfp:adapter> 

因此,API文檔之後,我做了以下調用中的適配器我離子供應商

@Injectable() 
export class MenuListingService { 
    data: any; 

    constructor() { 
    console.log('---> Constructing menu list adapter'); 
    this.data = null; 
    } 

    load(menuTitle: string) { 
    console.log('---> Request '+menuTitle+' Menu'); 
    if (this.data) { 
     // already loaded data 
     return Promise.resolve(this.data); 
    } 

    // don't have the data yet 
    return new Promise(resolve => { 

    let menuRequest = new WLResourceRequest("/adapters/menus/getSpecificMenu", WLResourceRequest.GET); 
    menuRequest.setQueryParameter('menuName', menuTitle); 

    menuRequest.send().then((response) => { 
      console.log('---> Current menu response received'); 
      this.data = response.responseJSON.offers; 
      resolve(this.data); 
     }) 

    }); 
    } 
} 

閱讀知識中心,我發現了一些關於cal使用查詢字符串來查找參數,例如?params = ['value'],但是500代碼失敗。

請記住,我不在辦公室,因此我將在明天添加一個編輯,以便爲您提供有關服務器響應和MFP服務器日誌條目的更多信息。

但是,到目前爲止,我所做的事情有什麼不對嗎?

+0

根據以下內容,應該不是這樣:'request.setQueryParameterValue(「['value1','value2']」,forName:「params」) '然後你在JavaScript中處理'params'適配器?請參閱:https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/using-the-mfpf-sdk/resource-request/ios/#javascript-adapters –

+0

Mhhh,我沒有選擇使用該選項方法。最後一個文件是一個打字稿文件,從我看到你使用的是快速文件。 –

+0

我錯過了什麼嗎?你的應用不是Swift應用嗎? –

回答

1

在這裏看到:https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/using-the-mfpf-sdk/resource-request/javascript/#setqueryparameter

更具體地說:

JavaScript的適配器使用下令無名參數。將參數傳遞給一個Javascript適配器,設置參數名爲PARAMS的數組: resourceRequest.setQueryParameter("params", "['value1', 'value2']");

什麼你缺少的是使用「PARAMS」,而不是menuRequest.setQueryParameter('menuName', menuTitle);的。

相關問題