我正在尝试发送一个ajax发送请求,但它总是进入error。
$('form#contactForm button.submit').click(function () {
var contactName = $('#contactForm #contactName').val();
var contactEmail = $('#contactForm #contactEmail').val();
var contactSubject = $('#contactForm #contactSubject').val();
var contactMessage = $('#contactForm #contactMessage').val();
var data = {
'contactName': contactName,
'contactEmail': contactEmail,
'contactSubject': contactSubject,
'contactMessage': contactMessage
};
$.ajax({
type: "POST",
url: "/",
data: data,
dataType: 'json',
contentType: "application/json",
success: function (msg) {
alert('success message: ' + msg);
if (msg == 'OK') {
$('#image-loader').fadeOut();
$('#message-warning').hide();
$('#contactForm').fadeOut();
$('#message-success').fadeIn();
$('#contactForm button.submit').prop('disabled', true);
}
else {
$('#image-loader').fadeOut();
$('#message-warning').html(msg);
$('#message-warning').fadeIn();
}
},
error: function (err) {
if (contactName.length === 0 || contactEmail.length === 0 || contactSubject.length === 0 ||
contactMessage.length === 0) {
$('#message-warning').html('Please check form once again.');
$('#message-warning').fadeIn();
event.preventDefault();
}
alert('inside error: ' + err.message);
}
});
return false;
});这总是在error中显示alert('inside error: ' + err.message);,.I也尝试过data: JSON.stringify(data),但也不起作用。data变量有问题吗?问题出在哪里?
网络选项卡:
Request URL:http://localhost:3000/
Request Method:POST
Status Code:400 Bad Request
Remote Address:[::1]:3000
Response Headers
view source
Connection:keep-alive
Content-Length:24
Content-Type:text/html; charset=utf-8
Date:Sun, 23 Oct 2016 00:12:43 GMT
ETag:W/"18-9LwX+BuZqYnTTqGm6GcNuA"
X-Powered-By:Express
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:102
Content-Type:application/json
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
contactName=My+Name&contactEmail=email%40email.com&contactSubject=My+Subject&contactMessage=My+Message后端post
router.post('/', function (req, res) {
if (typeof req.body.contactName === 'undefined' || typeof req.body.contactEmail === 'undefined' ||
typeof req.body.contactSubject === 'undefined' || typeof req.body.contactMessage === 'undefined' ||
!validator.isEmail(req.body.contactEmail)) {
return res.status(400).send('error');
}
mail.sendMail(req.body.contactName, req.body.contactEmail, req.body.contactSubject, req.body.contactMessage);
return res.status(200).send('okay');
});发布于 2016-10-23 08:56:57
我已经删除了dataType: 'json'和contentType: "application/json",ajax请求成功。我仍然不知道为什么,但它工作后,删除他们。
发布于 2016-10-23 08:09:01
dataType指的是来自服务器的预期响应-请参阅HERE。如果响应不是JSON,那么从ajax函数中删除dataType: 'json',jQuery将进行智能猜测。
发布于 2016-10-23 09:28:07
ajax客户端期望您的响应MIME类型为'json',但是您提供了一个纯文本‘you’作为响应。
如果您仍然需要发送json作为响应,您可以使用res.type更改您的后台,如下所示:
router.post('/', function (req, res) {
if (typeof req.body.contactName === 'undefined' || typeof req.body.contactEmail === 'undefined' ||
typeof req.body.contactSubject === 'undefined' || typeof req.body.contactMessage === 'undefined' ||
!validator.isEmail(req.body.contactEmail)) {
return res.status(400).type('json').send({"result": "error"});
}
mail.sendMail(req.body.contactName, req.body.contactEmail, req.body.contactSubject, req.body.contactMessage);
return res.status(200).type('json').send({"result":"okay"});
});那么你的前端应该会像预期的那样工作。在大多数情况下,不需要指定dataType,因为ajax可以根据服务器响应自动确定mime类型。
https://stackoverflow.com/questions/40198172
复制相似问题