我正在调用一个 php 文件,其中包含将数据保存到数据库中的类及其函数。 调用是通过ajax。 在 mozilla 中一切正常,而在 chrome 中则不行。 但非常令人惊讶的是,有时它会突然起作用,如果我再试一次或其他时间,它就会失败。 我已经验证了 php 文件是完美的。 甚至 ajax 代码也是...
$('#quick_contact').validate({
rules: {
qc_name: {
required: true,
minlength: 0
},
qc_email: {
required: true,
email: true
},
qc_msg: {
required: false,
minlength: 0
}
},
messages: {
name: {
required: "Enter Your Name",
},
email: {
required: "Please Enter Your e-mail"
},
message: {
required: "Please Enter Message"
}
},
submitHandler: function(form) {
$.ajax({
type:"POST",
url:root_path+"db_contact_forms/contact_forms.php",
data: $(form).serialize(),
dataType: "json",
async: false,
success: function(dres) {
alert(dres.status);
},
error: function(textStatus, errorThrown) {
alert(errorThrown);
}
});
}
});
所以在 chrome 中,它第一次将抛出错误作为解析错误,然后它就正确执行了。我很困惑。
PHP:
<?php
if(isset($_POST["qc_submit"]))
{
$contact_form = new contact_forms();
$contact_form->save();
echo '{"status":1}';
}
class contact_forms
{
public $con;
public function __construct()
{
$this->con = mysqli_connect("localhost", "root", "", "orione_db");
if(!$this->con)
{
echo "Not Connected";
}
}
function save()
{
$sql="insert into quick_contact_form(name, email, msg, datetime) values('".$_POST["qc_name"]."','".$_POST["qc_email"]."','".$_POST["qc_msg"]."', '".date('d-m-Y h:i:s')."')";
mysqli_query($this->con, $sql);
}
}
?>
HTML:
<form method="post" id="quick_contact" novalidate="novalidate">
<input type="text" class="form-control" id="name" name="qc_name" placeholder="Name">
<input type="email" class="form-control" id="email2" name="qc_email" placeholder="Email" style="margin-top:10px;">
<textarea class="form-control" rows="6" id="message" name="qc_msg" placeholder="Message" style="height: 134px;margin-top: 10px;"></textarea>
<div class="row m0">
<button type="submit" name="qc_submit" class="btn btn-default submit"><strong>Submit Now</strong><i class="fa fa-angle-double-right" style="padding-left:6px;"></i></button>
</div>
</form>
哪里做错了?我希望有人能提供帮助。
最佳答案
jQuery 的 serialize
不包括提交按钮。此外,由于未设置提交按钮时没有输出,因此请求需要 json 但什么也得不到并发出解析错误。
关于javascript - Ajax 请求在 mozilla 中完美运行而不在 chrome 中运行(非常困惑,因为有时它可以运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42440058/