I am a new user for amazon web service. Recently I am studying amazon lambda server and I have a small node.js code as below(following this example:http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html):

我是amazon web服务的新用户。最近我正在研究amazon lambda服务器,我有一个小节点。js代码如下(如下例:http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-star.html):

console.log('Loading event');

exports.handler = function(event, context) {
var name = (event.name === undefined ? 'No-Name' : event.name);
context.done(null, {"Hello":name}); // SUCCESS with message
};

But I don't know how to use jquery ajax method to pass "name" parameter to that code. It work when I use:

但是我不知道如何使用jquery ajax方法将“name”参数传递给该代码。当我使用:

curl -H "Content-Type: application/json" -X POST -d "{\"name\": \"PeterChan\"}" https://my-api-id.execute-api.region-id.amazonaws.com/test/mydemoresource

and I can get the result:{"Hello":"User"}

我可以得到结果:{"你好":"用户"}

but how can I use jquery ajax method to past "name" variable?

但是,我如何使用jquery ajax方法来忽略“name”变量呢?

The ajax code that I wrote:

我编写的ajax代码:

var data = {"name":"bbbb"};

 $.ajax({
      type: "POST",
      dataType: "json",
      url:"https://my-api-id.execute-api.region-id.amazonaws.com/test/mydemoresource",
      data: data,
      //processData: false,
      success: function(data) {
            var text=JSON.stringify(data);
            alert(text);
            //console.log(data);

      },error: function(data) {
            alert("err");
      }
});

It alert err when I run it. Pleas help.

我运行时它会出错。请求帮助。

Thank you~

谢谢你~

1 个解决方案

#1


4

So it looks like you might have a couple of issues. One, avoid using "name" as a variable name. Two, the data probably isn't being parsed correctly. You need JSON.Stringify when you send it (yeah, you've already got JSON, but it's finicky):

看起来你可能有几个问题。第一,避免使用“name”作为变量名。第二,数据可能没有被正确解析。你需要JSON。当你发送它时Stringify(是的,你已经有JSON了,但是它很挑剔):

$.ajax({
    url: 'https://mylambdafunctionurl/',
    type: 'POST',
    crossDomain: true,
    contentType: 'application/json',
    data: JSON.stringify(data),
    dataType: 'json',
    success: function(data) {
        //success stuff. data here is the response, not your original data
    },
    error: function(xhr, ajaxOptions, thrownError) {
        //error handling stuff
    }
});

I've also added crossDomain:true, and contentType: 'application/json'.

我还添加了crossDomain:true和contentType: 'application/json'。

In the lambda function to get the key/value in the passed in JSON you just use event.whateverkey (when using the test event in the Lambda console make the keys match what you're sending in to avoid any problems).

在lambda函数中,只需使用event就可以在传入的JSON中获取键/值。whateverkey(当在Lambda控制台使用测试事件时,使密钥与您所发送的内容相匹配,以避免出现任何问题)。

The data in your success callback in your ajax function is what comes back from the lambda function, so I recommend JSON.stringifying that in the lambda function, and not the success to make sure it gets sent correctly:

ajax函数中的成功回调中的数据来自lambda函数,因此我推荐JSON。限制在lambda函数中,而不是成功地确保它被正确地发送:

context.done(null, JSON.stringify({"Hello":name}));

更多相关文章

  1. 在each()函数内部调用多个ajax ..然后在完成所有这些操作后执行某
  2. 为什么使用observe_field代码不能使用JQuery模型对话框?
  3. 加载函数加载页面但不能对数据执行任何操作
  4. 绑定和解除相同javascript函数的目的是什么?
  5. Jquery中的队列函数quene()、dequene()、clearQuene()
  6. jquery 生成table表格 部分代码
  7. Ajax_04之jQuery中封装的Ajax函数
  8. 怎么用js或jquery把一个函数b绑定到另一个函数a之后执行
  9. 使用jquery的$ .ajax来获取node.js脚本的输出,而不是它的代码

随机推荐

  1. 手机上利用python进行数据分析——创建自
  2. python数据分析——在python中实现线性回
  3. 海象运算符?一文详解python3.8到底更新了
  4. Centos6.X虚拟机配置Hadoop2.6的步骤
  5. 说一点学习python的心得
  6. 动画:二叉树在实际中的应用(下)
  7. 钉钉在线求饶?五星分期付款?爬取钉钉App St
  8. python中那些小众但有用的内置模块
  9. 动画:如何给面试官回答 TCP 的拥塞控制?
  10. python数据分析之清洗数据:缺失值处理