使用jQuery对Python中的AWS lambda函数进行Ajax调用

编程入门 行业动态 更新时间:2024-10-07 02:17:56
本文介绍了使用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="ajax.googleapis/ajax/libs/jquery/2.2.4/jquery.min.js"></script> </head> <body> <script> window.alert( "before" ); </script> <script> $.ajax( { url: 'npvkf9jnqb.execute-api.us-east-1.amazonaws/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:

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

Failed to load npvkf9jnqb.execute-api.us-east-1.amazonaws/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: 'npvkf9jnqb.execute-api.us-east-1.amazonaws/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调用

本文发布于:2023-11-28 06:16:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Python   jQuery   AWS   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!