2

I seem to be getting a issue with Amazon Gateway API not liking my sent params for example.

$.ajax({
        url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
        type: "POST",
        data: {
            "device": "test",
            "datetime": "1446757400919"
        },
        success: function (returnhtml) {
            console.log(returnhtml);
            $("#result").append("DOES NOT WORK - <br>" + JSON.stringify(returnhtml));
        }
    });

    $.ajax({
        url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
        type: "POST",
        data: {},
        success: function (returnhtml) {
            console.log(returnhtml);
            $("#result").append("<br>WORKS ???? - <br>" + JSON.stringify(returnhtml));
        }
    });

Here is a working example. http://jsfiddle.net/Uwcuz/4315/

Can someone let me know why it wont let me send params every time i add a parameter i get this error.

{
    Type = User;
    message = "Could not parse request body into json.";
} 

OK THIS WORKS BUT SEEMS A BIT WEIRD TO ME.

$.ajax({
        url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
        type: "POST",
        data: "{\"device\": \"test\",\"datetime\": \"1446757444524\"}",
        success: function (returnhtml) {
            console.log(returnhtml);
            $("#result").append("WORKS - <br>" + JSON.stringify(returnhtml));
        }
    });

1 Answer 1

6

The problem lies in how you send the data to API Gateway. Without knowing the details of your API configuration I am guessing that you have a Request Mapping setup for application/json. jQuery will by default post your data as application/x-www-form-urlencoded but you want to send it as json.

You can do this without having to fiddle too much with the data yourself:

var requestParams = {
    url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
    method: "POST,
    contentType: "application/json",
    dataType: "json",
    data: JSON.stringify({
        "device": "test",
        "datetime": "1446757400919"
    });
};
var request = $.ajax(requestParams);

The key here is JSON.stringify() and telling jQuery that the dataType is json as well as setting the contentType to application/json.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.