jquery - Consume Amazon dynamoDB with JavaScript - Stack Overflow

I've been trying to interact with the Amazon DynamoDB via JavaScript using jQuery and an Ajax call

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 mand to dynamo in this link:

.html#JSONMajorExample

I've been able to do it with the PHP sdk and with node.js (), 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');
                 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: "",  
      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)

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 mand to dynamo in this link:

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

I've been able to do it with the PHP sdk and with node.js (https://github./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.');
                 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.",  
      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)

Share edited Feb 11, 2012 at 8:08 skolima 32.8k27 gold badges118 silver badges152 bronze badges asked Feb 10, 2012 at 23:57 ANet DevANet Dev 311 silver badge2 bronze badges 3
  • Let me get it right... Do you want to call the DB directly from the UI without any server-side validation? – benqus Commented Feb 11, 2012 at 0:03
  • Hi Benqus, Thanks for your answer. Yes I am trying to hit dynamodb without any server-side validation or server scripts (php, , ruby, etc). I would like to do everything with just javascript. – ANet Dev Commented Feb 13, 2012 at 21:48
  • I really don't wish to demotivate you but that seems VERY UNSAFE to me... I don't know, maybe I am too cautious but you shouldn't do that... – benqus Commented Mar 15, 2012 at 9:08
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744917642a4600933.html

相关推荐

  • jquery - Consume Amazon dynamoDB with JavaScript - Stack Overflow

    I've been trying to interact with the Amazon DynamoDB via JavaScript using jQuery and an Ajax call

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信