ANet Dev
ANet Dev

Reputation: 31

Consume Amazon dynamoDB with JavaScript

I've been trying to interact with the Amazon DynamoDB via JavaScript using jQuery and an Ajax call but have been unsuccessful. After two days of research I am beginning to thing it may not be possible. I see that they have SDKs available for Java, PHP, and .Net, but nothing for JavaScript yet.

Amazon explains how to send a command to dynamo in this link:

http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/UsingJSON.html#JSONMajorExample

I've been able to do it with the PHP sdk and with node.js (https://github.com/xiepeng/dynamoDB), but no luck with a regular javascript ajax call or xmlHttpRequest call.

I have been able to get a valid aws signature, secret id, and session token, so I have hard coded those into the headers.

Here is my code:

$.ajax({  
    beforeSend: function(xhr) {
                console.log("getting built");


                     xhr.setRequestHeader('host', 'dynamodb.us-east-1.amazonaws.com');
                 xhr.setRequestHeader('x-amz-date', 'Fri, 10 Feb 2012 20:44:00 GMT');
                 xhr.setRequestHeader('date', 'Fri, 10 Feb 2012 20:44:00 GMT');
                 xhr.setRequestHeader('x-amz-security-token', '**MYSECURITY TOKEN**');
                 xhr.setRequestHeader('x-amz-target', 'DynamoDB_20111205.PutItem');
                 xhr.setRequestHeader('content-type', 'application/x-amz-json-1.0');
                 xhr.setRequestHeader('content-length', 103);
                 xhr.setRequestHeader('x-amzn-authorization', 'AWS3 AWSAccessKeyId=**MY ACCESS KEY**,Algorithm=HmacSHA256,SignedHeaders=host;x-amz-date;x-amz-security-token;x-amz-target,Signature=**MY SIGNATIURE**=');


          },

    type: "POST",  
    url: "http://dynamodb.us-east-1.amazonaws.com",  
      dataType: "json",
    data: '{"TableName":"Sample","Item":{"RecordId":{"S":"white"},"Square":{"S":"teess"},"circle":{"S":"eeerer"}}}',
        error: function(XHR,textStatus,errorThrown) {
    //  alert ("XHR="+XHR+"\ntextStatus="+textStatus+"\nerrorThrown=" + errorThrown);
            console.log(XHR);
        console.log(textStatus);
          console.log(errorThrown);
        },

        success: function(data) { 
                console.log("success");
            }
        });

When I run this I get a 404 Not found error, with the method showing as "OPTIONS" (as opposed to POST or GET)

Upvotes: 3

Views: 3980

Answers (2)

Jeremy Lindblom
Jeremy Lindblom

Reputation: 6517

You are attempting to make a cross-domain request with AJAX. This is not something that necessarily works unless both your application and the service are setup for it. AWS does not currently allow requests via the CORS protocol. The OPTIONS header that you saw is your JavaScript making a pre-flight CORS request to AWS, which is being rejected. You will need to use a server-side proxy (which uses one of the SDKs provided by AWS) to make the actual service calls. Your JavaScript can talk to your proxy via AJAX since it will be hosted on the same domain.

Upvotes: 3

daniel0mullins
daniel0mullins

Reputation: 1959

Not very familiar with AWS Dynamo, but am very familiar with HTTP and XMLHttpRequest and Host is not a header that you can set via xhr. XHR pulls the host info from the url that is being requested. Not sure if $.ajax will ignore you trying to set that header or not, but I would try it without it.

Also, how are you calculating your content length? Your string there is 103 characters, but it is not necessarily 103 bytes (depending on encoding, charset, etc), which is how Content-Length is calculated. I would try it without that header as well.

Let us know how it goes!

UPDATE:

I think is falling victim to the 'Same-Origin Policy' that has been a part of Ajax since Microsoft made that decision for everyone. :-) You're going to have to code some sort of server-side proxy that resides on your domain, and make the Ajax requests to/from that.

Are you familiar with PHP? It looks like AWS has a lib for DynamoDB in PHP.

Upvotes: 3

Related Questions