【发布时间】:2016-04-27 06:45:12
【问题描述】:
我检查了很多关于这个问题的帖子并尝试了不同的解决方案,但没有一个对我有用,我的 AJAX POST 正在工作,它只是将电子邮件添加到我的电子邮件列表中,但我的回拨成功和错误都没有在未知中触发期望条件,或者我可以(随机)调用它。
HTML:
<form name="myform">
<input id="user_email" type="email" name="email" placeholder="Your Email Here" required>
<button type="submit" onclick="onbtnclick()">SIGN UP</button>
</form>
AJAX:
$.ajax({
type: 'POST',
url: 'http://mail.ayaami.com/freebook_10x10.php',
crossDomain: true,
data: send_data,
success: function(responseData, textStatus, jqXHR) {
alert("Thank you for joining Ayaami mailing list.");
window.open("http://dev.ayaami.com/site/free-book-success");
// var value = responseData.someKey;
// console.log(' log d: '+responseData);
// console.log(' log d: '+textStatus);
// console.log(' log d: '+jqXHR);
// var obj = jQuery.parseJSON(responseData);
// hide the email box
// text_respond="<p>"+obj.result+"</p>";
// set cookies
var cookieName = 'ayaami_newsletter_lightbox';
var cookieValue = 'true';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + (500 + 12));
document.cookie = cookieName + "=" + cookieValue + ";expires=" + myDate +
";domain=.ayaami.com;path=/";
},
error: function(responseData, textStatus, errorThrown) {
alert('You already subscribed in the mailing list.');
console.log('log e:' + responseData);
console.log('log e:' + textStatus);
console.log('log e:' + errorThrown);
}
});
我尝试成功弹出警报或重定向到另一个问候页面,但没有成功,我删除了之前在 AJAX 参数中的数据类型仍然没有任何改变。
版本,用 document.ready 包装
$( document ).ready(function() {
$('#mybtn').click(function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://mail.ayaami.com/freebook_10x10.php',
crossDomain: true,
data: {email:$(this).parent().find('input').val()},
success: function(responseData, textStatus, jqXHR) {
alert("Thank you for joining Ayaami mailing list.");
window.open("http://dev.ayaami.com/site/free-book-success");
// var value = responseData.someKey;
// console.log(' log d: '+responseData);
// console.log(' log d: '+textStatus);
// console.log(' log d: '+jqXHR);
// var obj = jQuery.parseJSON(responseData);
// hide the email box
// text_respond="<p>"+obj.result+"</p>";
// set cookies
var cookieName = 'ayaami_newsletter_lightbox';
var cookieValue = 'true';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + (500 + 12));
document.cookie = cookieName + "=" + cookieValue + ";expires=" + myDate +
";domain=.ayaami.com;path=/";
},
error: function(responseData, textStatus, errorThrown) {
alert('You already subscribed in the mailing list.');
console.log('log e:' + responseData);
console.log('log e:' + textStatus);
console.log('log e:' + errorThrown);
}
});
});
}
【问题讨论】:
-
如果要重定向到另一个页面,为什么要使用 ajax?
-
可以添加onbtnclick函数的代码吗?
-
@madalinivascu 这是我想到的解决方案之一,毕竟我需要给用户一个反馈,无论我给他们警报还是新页面,他们是否可以注册。
-
@dimlucas "onbtnclick" 函数中的代码。
-
@SadiqJaffer 你在控制台有什么错误?
标签: javascript jquery ajax post