使用jQuery对Python中的AWS lambda函数进行Ajax调用 [英] Ajax call using jQuery to AWS lambda function in Python

查看:144
本文介绍了使用jQuery对Python中的AWS lambda函数进行Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图让我了解ajax调用和aws lambda的问题,但是我一直在努力奋斗了几个小时,我想出了最简单的示例:只是让javascript/jquery对lambda进行ajax调用,让lambda返回文字"blah",然后在浏览器的警报中打印出来.

请帮忙!

我已经创建了一个lambda函数,其中的post似乎可以正常工作(当我在浏览器中转到url时,在浏览器中看到了空白):


I'm trying to get my head around ajax calls and aws lambda, but I've been struggling for hours with the most simple example I could think of: just to have javascript / jquery do an ajax call to lambda, have lambda return the text 'blah' and then print that in an alert in my browser.

Please help!

I have created a lambda function with post that seems to work (when I go to the url in my browser, I see blahh in my browser):

def lambda_handler(a, b):
    return({
        "isBase64Encoded": True,
        "statusCode": 200,
        "headers": { "headerName": "headerValue"},
        "body": "blahhh"
    })

我的html文件如下:

And my html file is as follows:

<!doctype html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>  
</head>

<body>
  <script> window.alert( "before" ); </script>
  <script>
    $.ajax(
        {
        url: 'https://npvkf9jnqb.execute-api.us-east-1.amazonaws.com/v1',
        type:'GET',
        dataType: 'text',
        success: function(data) {
            window.alert(data);
        }
    });


    window.alert( "after" );
    </script>

</body>
</html>

我的API网关设置是:在我们东部1的区域中,使用Lambda Integration集成类型为Lambda Function的GET方法(以及具有相同配置的POST),指向mylambdafunction(上面已写).其余所有默认情况下.我确实启用了CORS.

My API Gateway settings are: a GET method (and a POST with the same configuration) with intergration type Lambda Function, Using Lambda Integration, in the region us east 1, pointing to mylambdafunction (which is written above). All the rest is on default. I did enabe CORS.

日志看起来像是以下内容的连续重复:

The logs look like a continuous repetition of the following:

START RequestId: 40847960-c98f-11e8-9191-818092ca5731 Version: $LATEST
END RequestId: 40847960-c98f-11e8-9191-818092ca5731
REPORT RequestId: 40847960-c98f-11e8-9191-818092ca5731  Duration: 0.37 ms    
Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB  
START RequestId: 499d769b-c990-11e8-8ba2-2568c94a15d7 Version: $LATEST
END RequestId: 499d769b-c990-11e8-8ba2-2568c94a15d7
REPORT RequestId: 499d769b-c990-11e8-8ba2-2568c94a15d7  Duration: 1.18 ms    
Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB  
.....

但是,当我在浏览器中打开.html文件时,它似乎没有接替ajax部分.我想念什么?欢迎您提供任何帮助,因为我是一个完整的初学者!

But when I open the .html file in my browser it doesn't seem to succeed the ajax part. What am I missing? Any help is welcome, since I'm a complete beginner at this!

推荐答案

我将总结在错误的步骤中找到的解决方案:

I will summarize the solution I found in thee steps for the error:

无法加载 https://npvkf9jnqb.execute-api.us-east-1.amazonaws.com/v1 :所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问原始空".万一有人有同样的问题.

Failed to load https://npvkf9jnqb.execute-api.us-east-1.amazonaws.com/v1: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. in case anyone has the same problem.

1-必须启用Cors,并且必须指定'Access-Control-Allow-Origin':'*'这是默认设置.可以在AWS API Gateway设置中找到此设置.

1 - Cors has to be enabled and it is necessary to specify 'Access-Control-Allow-Origin':'*' This is by default. This setting can be found in AWS API Gateway settings.

2-Ajax调用应包含标题'Access-Control-Allow-Origin':'*'.这是在html文件中.

2 -The Ajax call should contain the header 'Access-Control-Allow-Origin':'*'. This is inside the html file.

$.ajax(
{
    url: 'https://npvkf9jnqb.execute-api.us-east-1.amazonaws.com/v1',
    headers: {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'
},
    crossDomain: true,
    type:'GET',
    dataType: 'text',
    success: function(data)
    {
        window.alert(data);
    }
}); `

3-Lambda函数还需要返回标头'Access-Control-Allow-Origin':'*'.这应该在AWS Lambda中完成.

3 - The Lambda function also needs to return the header 'Access-Control-Allow-Origin':'*'. This should be done in AWS Lambda.

def lambda_handler(a, b):
    return({
    "isBase64Encoded": True,
    "statusCode": 200,
    "headers": { 'Access-Control-Allow-Origin': '*'},
    "body": "blahhh"
    })

这篇关于使用jQuery对Python中的AWS lambda函数进行Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆