0

I'm trying to setup an API Gateway REST API that prompts a Lambda function and returns some items from a DynamoDB table.

When I test the API in the AWS API Gateway console I am getting the correct data back, but getting a Unexpected end of JSON input error trying in JavaScript (just trying in JS Fiddle).

Here is my fetch function:

function fetchData(){
    fetch("https://abcd1234.execute-api.region.amazonaws.com/stage/product?productId=1234"
    ).then(response => {
        return response.json()
    }).then(jsonData => {
        console.log('success:', jsonData)
    }).catch(e => {
        console.log('error:', e.message)
        return e
    })
}

It's a get method in the Lambda function and I'm newer to this, so not sure what I'm doing wrong. If it was a CORS issue would I get a different message back?

Yolo_chicken
  • 1,221
  • 2
  • 12
  • 24
  • 1
    Logging `response` won't be all that useful, resolve the promise returned by `response.text()` instead. – Quentin May 19 '23 at 13:59
  • Thanks. When I change `response.json()` to `response.text()` I get `""`. Does this indicate the API isn't returning the data I expect? – Yolo_chicken May 19 '23 at 14:13
  • Yes. It seems to be returning nothing. – Quentin May 19 '23 at 14:13
  • When I look at the URL in the response, it's not showing the complete API URL. Getting cut off at a `#` character. Is there any reason I'm not seeing the full URL in the response? – Yolo_chicken May 19 '23 at 14:22
  • 1
    Duplicate: https://stackoverflow.com/questions/45321834/script-url-with-pound-sign-hashtag-cut-off-when-inserting-in-dom – Quentin May 19 '23 at 14:33
  • This was it all along. And should be the accepted answer. – Yolo_chicken May 19 '23 at 14:37

0 Answers0