5

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?
4

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);
    }
});
3
  • How do I hide my username and password (api user and api key)? If it's javascript, can't people just view the js file/code in plaintext? Should I be worried? – Katie Jan 26 '17 at 23:48
  • I asked a new question about the username and password, available here. I'm still not sure how I'm going to hide my api username and key. – Katie Jan 27 '17 at 21:26
  • 1
    If you are using only js then you can't. I think you can put this whole code on the server and call that. Call that returns only session id so your credential on the server. – jaydip sinh Parmar Jan 28 '17 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.