Search code examples
javascriptjsonember.jsember-datajson-rpc

Ember Data JSON-RPC requests Example


I would like to do Post requests to my zabbix server using EmberJS and ember-data

How would I go about querying with JSON-RPC ?

In python I'd do something like this:

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))

Solution

  • In ember.js you can use ember-ajax, which is normally included in a new project.

    Transforming your python code above to ember-ajax should look like the following (when used in a controller):

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