![](/img/trans.png)
[英]jquery ajax call returning an error with readystate 4, status 200, statustext ok
[英]nodejs https status 200 but body response null, ajax call works but returning error status 0
我正在使用 aws 文檔中的代碼在 static 站點上實現聯系表單。 當我測試nodejs lambda function 它是成功的,但返回的結果是null
。
API 網關中的 POST 測試結果相同。 它返回:
HTTP 狀態:200
響應正文:null
對於方法執行它說Models: application/json => Empty
。 有問題嗎? 如果是這樣,我不確定要更改什么。
var AWS = require('aws-sdk');
var ses = new AWS.SES();
var RECEIVER = 'name@example.com';
var SENDER = 'name@example.com';
var response = {
"isBase64Encoded": false,
"headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'example.com'},
"statusCode": 200,
"body": "{\"result\": \"Success.\"}"
};
exports.handler = function (event, context) {
console.log('Received event:', event);
sendEmail(event, function (err, data) {
context.done(err, null);
});
};
function sendEmail (event, done) {
var params = {
Destination: {
ToAddresses: [
RECEIVER
]
},
Message: {
Body: {
Text: {
Data: 'name: ' + event.name + '\nphone: ' + event.phone + '\nemail: ' + event.email + '\nnote: ' + event.note,
Charset: 'UTF-8'
}
},
Subject: {
Data: 'Website Contact Form: ' + event.name,
Charset: 'UTF-8'
}
},
Source: SENDER
};
ses.sendEmail(params, done);
}
From the html side, the contact form works as expected when I run the script (an email is delivered to my inbox with name, email, and message) but the ajax request returns an error status 0 HTTP Error: 0
function submitToAPI(e) {
e.preventDefault();
var URL = "https://Restful API URL";
var name = /[A-Za-z]{1}[A-Za-z]/;
if (!name.test($("#name-input").val())) {
alert ("Name cannot be less than 2 characters");
return;
var email = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;
if (!email.test($("#email-input").val())) {
alert ("Please enter valid email address");
return;
}
var name = $("#name-input").val();
var email = $("#email-input").val();
var note= $("#note-input").val();
var data = {
name : name,
email : email,
note: note
};
$.ajax({
type: "POST",
url : "https://Restful API URL",
dataType: "json",
crossDomain: "true",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
success: function () {
alert("Thank you!");
document.getElementById("contact-form").reset();
location.reload();
},
error: function (response) {
alert("HTTP Error: " + response.status);
document.getElementById("contact-form").reset();
location.reload();
}});
}
我在這里搜索了論壇並嘗試刪除dataType: "json"
並且還檢查了 CORS 已啟用。 都沒有修好。 我被困住了。 誰能指出我正確的方向?
在我看來,您是在告訴您的 lambda 在回調中返回null
。 您可以改為返回您的響應變量:
exports.handler = function (event, context) {
console.log('Received event:', event);
sendEmail(event, function (err, data) {
context.done(err, response); // <--- change null to response
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.