gpt4 book ai didi

javascript - $.ajax 没有返回响应

转载 作者:行者123 更新时间:2023-11-27 23:11:55 27 4
gpt4 key购买 nike

您好,我在获取ajax请求的响应时遇到问题,我已经这样做了很多次,但我不知道现在的问题是什么,我已经在这个问题上拉扯我的头发一天了,我尝试过使用完整、错误或.done,但没有任何效果。我正在做的是通过ajax插入数据,然后在插入数据时获取响应,现在数据插入正常但我没有得到响应,你能帮助我吗,并且没有控制台错误。

insert_ajax.php

<?php
?>
<html>
<head>
<title>Basic Ajax Insert</title>
<link rel="stylesheet" href="style/style.css" type="text/css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css"/>
<script src="jquery-1.3.1.min.js"></script>
<script src="custom.js"></script>
</head>
<body>
<div class="container">
<div id="succ"></div>
<h2 class="text-warning">Basic Information</h2>
<form id="form" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" autocomplete="off">

<div class="form-group col-md-6">
<label class="text-warning">First Name: </label>
<input class="form-control first_name" type="text" name="fname" />
</div>

<div class="form-group col-md-6">
<label class="text-warning">Last Name: </label>
<input class="form-control last_name" type="text" name="lname" />
</div>

<div class="form-group col-md-6">
<label class="text-warning">Email: </label>
<input class="form-control email" type="text" name="email" />
</div>
<div class="form-group col-md-6">
<label class="text-warning">Password: </label>
<input class="form-control pwd" type="password" name="pass" />
</div>

<button class="btn btn-warning" name="btn_sub">Send</button>

</form>
</div>
</body>
</html>

ajax.php

<?php

$con=mysqli_connect("localhost","root","","practices");

if(isset($_REQUEST['em'])){
echo'email received..: '.$_REQUEST['em'];
}

$fname=$_REQUEST["fn"];
$lname=$_REQUEST["ln"];
$email=$_REQUEST["em"];
$pass=$_REQUEST["pwd"];
//echo "returned";
$ins="insert into `user` (`first_name`,`last_name`,`email`,`password`)
values('$fname','$lname','$email','$pass')";
$query=mysqli_query($con,$ins);

if($query){
echo "Data is entered Love";
}
?>

自定义.js

$(document).ready(function(){

$(".btn").click(function(){

var fname=$(".first_name").val();
var lname=$(".last_name").val();
var email=$(".email").val();
var pass=$(".pwd").val();

$.ajax({
method: "POST",
url : "ajax.php",
data: {fn:fname,ln:lname,em:email,pwd:pass},
success: function(r){
alert(r);
}

});



});

});

最佳答案

更改为这些行后尝试,

$(".btn").on("click",function(e){
e.preventDefault();
var fname=$(".first_name").val();
var lname=$(".last_name").val();
var email=$(".email").val();
var pass=$(".pwd").val();

$.ajax({
method: "POST",
url : "ajax.php",
data: {fn:fname,ln:lname,em:email,pwd:pass},
success: function(ind,val){
console.log(ind);
console.log(val);
}

});

关于javascript - $.ajax 没有返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36114907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com