Using Magento 1.9 REST API with JavaScript – A Guide

apiappmagento-1.9

I want to create Magento store Android app using PhoneGap. I found Magento's XML-RPC is the best option to create an API but now how I can call this API using only JavaScript and HTML ?

So it is sure my case I have to use JavaScript to use the API.So there is two options I think

  1. If yes then how to use ?
  2. if No then what's option?

Best Answer

Yes, you can

you can call magento's xmlrpc apis using jQuery XML-RPC library.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script src="js/jquery.xmlrpc.js"></script>

first is simple jquery library and second library file you can get from here:

http://jquery-xml-rpc.readthedocs.io/en/latest/index.html

Here is two type of example of this call.

1. Login Call

$.xmlrpc({
    url: 'Your_magento_root/index.php/api/xmlrpc',
    methodName: 'login',
    params: ['api_user', "api_key"],
    success: function(response, status, jqXHR) {
        alert('Sucsses');       
        Session = response[0]; // this session key is important for next transaction                 
    },
    error: function(jqXHR, status, error) {
        alert('error');
        console.log(status);  
    }
});

2. After Login Calls

$.xmlrpc({
    url: 'Your_magento_root/index.php/api/xmlrpc',
    methodName: 'call',
    params: [Session, 'Your_methode_name',[0,'any other parameter',1]],
    success: function(response, status, jqXHR) {
        alert('Sucsses');
        var data = response[0];  // Here is your data.
    },
    error: function(jqXHR, status, error) {
        alert('error-inside');
        console.log(error);
    }
});