400错误POST方法到DynamoDB - AJAX

时间:2018-04-09 21:51:08

标签: json ajax amazon-web-services aws-lambda amazon-dynamodb

我目前正在尝试使用我添加到HTML表单中的信息更新DynamoDB表。每次我运行我的程序并提交我的信息时,都会收到此错误:

  

POST https://quovje1gi8.execute-api.us-east-2.amazonaws.com/production/accounts 400()

我可以在AWS上运行我的Lambda函数,并将数据添加到我的表中。我一次又一次地检查,以确保我的权限已正确建立,他们是。我似乎无法解决这个问题。

我的前端:

<form>
<label for="username">Add Username:</label>
<textarea id="username"></textarea>

<button id='submitButton'>Submit</button>
</form>

<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> 
</script>
<script type="text/javascript">
var API_URL = 'https://quovje1gi8.execute-api.us-east-2.amazonaws.com/production/accounts';

$(document).ready(function(){
    $.ajax({
        type:'GET',
        url: API_URL,

        success: function(data){
            $('#accounts').html('');

            data.Items.forEach(function(accountsItem){
                $('#accounts').append('<p>' + accountsItem.username + '</p>');
            })
        }
   });
});

$('#submitButton').on('click', function(){
    $.ajax({
        type:'POST',
        url: API_URL,
        data: JSON.stringify({"username": $('#username').val()}),
        contentType: "application/json",

        success: function(data){
            location.reload();
        }
   });

    return false;

});

Lambda函数:

'use strict';

console.log('Starting Function');

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region:'us-east-2'});

exports.handler = function(e, ctx, callback) {

var params = {
    Item: {
        user_id: ,
        date: Date.now(),
        username: "",
        password: "",
        name: "",
        location: "",
        field: "",
        company: ""
    },

    TableName: 'accounts'
};

docClient.put(params, function(err, data){
    if(err){
        callback(err, null);
    }
    else{
        callback(null, data);
    }
});

}

0 个答案:

没有答案