1

I want to run Magento Apis using JavaScript and person certain functions such: get all orders, get specific order, create shipment etc,

Now I have seen other tutorials and i can run Magento Apis through PHP. but i haven't found a much things on running Apis through JS or other languages other than PHP. is there any way.

Kindly help

1

The Magento API supports SOAP and XML-RPC, where SOAP is the default protocol.

You can do this using Magentos XML-RPC.

To use XML-RPC, load the following URL into your XML-RPC client:

http://magentohost/api/xmlrpc/

Reference Examples:

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

You can get jQuery XML-RPC library from here:

jQuery XML-RPC library

Login Call for Session token

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

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

Reference Doc: https://devdocs.magento.com/guides/m1x/api/soap/introduction.html

I hope this will help

| improve this answer | |
  • getting this error in browser m1-test.html:1 Access to XMLHttpRequest at 'host.com/index.php/api/xmlrpc' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request. – Joey Leblanc Jul 31 '19 at 13:42
  • This is not working, if you know the way around. Kindly tell. – Joey Leblanc Jul 31 '19 at 14:09
  • Hi Hasham, can you help me on this – Joey Leblanc Aug 1 '19 at 9:58

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.