AWS Lambda not working from AJAX

Viewed 980

I have a working AWS lambda (deployed and functioning), serving HTTPS responses via API gateways, that I am now trying to incorporate into an html page. But the ajax keeps failing for this URL (works fine for a local file, for example).

The lambda's output when called using curl -v:

* TCP_NODELAY set
* Connected to ....execute-api.eu-central-1.amazonaws.com (54.230.197.250) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.execute-api.eu-central-1.amazonaws.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> GET /XXX HTTP/1.1
> Host: ....execute-api.eu-central-1.amazonaws.com
> User-Agent: curl/7.51.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 65
< Connection: keep-alive
< Date: Wed, 12 Jul 2017 11:55:26 GMT
< x-amzn-RequestId: fe78ee4b-66f8-11e7-a1ac-470fc8454a23
< Access-Control-Allow-Origin: *
< X-Amzn-Trace-Id: sampled=0;root=1-59660e2e-9a6e70cc6d287d90115f73df
< Access-Control-Allow-Credentials: true
< X-Cache: Miss from cloudfront
< Via: 1.1 8ff53834d03bf51be6d4cf56d4e91f1d.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: pgseseaEv_Y12mzVCY07q2EfKOy9SSn_ztmMxvpKmBRPkTDXCjsJ6A==
< 
* Curl_http_done: called premature == 0
* Connection #0 to host ....execute-api.eu-central-1.amazonaws.com left intact

The ajax call in my js file is as follows:

$.ajax({
    url: "https://....execute-api.eu-central-1.amazonaws.com/XXX",
    contentType: "application/json", 
    success: function(result) {
    document.write(result);
    console.log(result);

}});

(Please assume that the URLs are the same on the curl and ajax calls)

Now - I never see the result on the webpage or console, but if I replace the lambda's URL with a local README.md file - I do see it there. On the other hand, calling the lambda's url on curl or in the browser shows me the output of my lambda very nicely.

What am I missing here? Thanks!

1 Answers
Related