2017-05-11 137 views
1

我願做POST請求使用EmberJS和灰燼數據灰燼數據JSON-RPC請求示例

我怎麼會去用JSON-RPC查詢我的ZABBIX服務器?

在Python中我會做這樣的事情:

HEADERS = {'content-type': 'application/json'} 
URL = 'http://zabbix.mydomain.com/zabbix/api_jsonrpc.php' 
QHEAD = { "jsonrpc": "2.0"} 

pl = QHEAD 
pl['id'] = "8" 
pl['method'] = "user.login" 
pl["params"] = { "user": "ZabbAdmin001", "password": "NunYaBeez.001." } 
r = requests.post(URL,headers=HEADERS,data=json.dumps(pl)) 

回答

1

在ember.js你可以使用ember-ajax,通常包含在一個新的項目。

轉化Python代碼上面燼阿賈克斯應該像(在控制器中使用時)以下內容:

import Ember from 'ember'; 

const { 
    get, 
    Controller, 
    inject: { service } 
} = Ember; 

export default Controller.extend({ 
    ajax: service(), 
    actions: { 
    sendRequest() { 
     return get(this, 'ajax').request('http://zabbix.mydomain.com/zabbix/api_jsonrpc.php', { 
     method: 'POST', 
     data: { 
      "jsonrpc": "2.0", 
      "id": 8, 
      "method": "user.login", 
      "params": { "user": "ZabbAdmin001", "password": "xxxxxxx" } 
     } 
     }).then(r => { 
     // Now r is your response 
     console.log(r); 
     }); 
    } 
    } 
});