0

I have tried 2 types of ajax calls to post json data so that i can order an item using woocommerce API. I am using OAuth1.0a to authorize the user. Its getting the data but not able to post the data. But none of them worked for me. So please somebody say how to post json data using ajax call. Thanks in advance.

var oauth2 = OAuth({
        consumer: {
            public: 'key',
            secret: 'secret'
        },
        signature_method: 'HMAC-SHA1'
    });

    var token = {
        public: 'key',
        secret: 'secret'
    };

    var dataToSend = {};

    var request_data = {
        url: 'http://www.example.com/wc-api/v3/orders',
        method: 'POST',
        data: dataToSend
    };

    return $.ajax({
        url: request_data.url,
        type: request_data.method,
        data: oauth2.authorize(request_data, token)
    });

    return $.ajax({
        url: request_data.url,
        type: request_data.method,
        'content-type': 'application/json',
        body: request_data.data,
        headers: oauth2.toHeader(oauth2.authorize(request_data, token))
    });

dataToSend - Example Data

key is genetrated using below link : https://docs.woothemes.com/document/woocommerce-rest-api/

algae514
  • 93
  • 9
SV Madhava Reddy
  • 1,858
  • 2
  • 15
  • 33

1 Answers1

0

Just simply see all the headers in the oauth2.authorize(request_data, token)
and simply add all those headers to the url after '?'...That's the solution i had...


    var generatedoauth = oauth.authorize(request_data, token);
    console.log("oauth " + JSON.stringify(generatedoauth));


    var newUrl = '<Url Goes Here>';
    var paramsSt = '?oauth_consumer_key=' + generatedoauth.oauth_consumer_key + '&oauth_nonce=' + generatedoauth.oauth_nonce + '&oauth_signature_method=' + generatedoauth.oauth_signature_method + '&oauth_timestamp=' + generatedoauth.oauth_timestamp + '&oauth_signature=' + generatedoauth.oauth_signature;
    newUrl = newUrl + paramsSt;`
SV Madhava Reddy
  • 1,858
  • 2
  • 15
  • 33