[英]How to do a post method using a jQuery ajax
I'm trying to make a simple post method using jquery ajax. 我正在尝试使用jquery ajax创建一个简单的post方法。 Whenever I click submit button, Im getting an error:400(Bad request) in my console. 每当我单击提交按钮时,我的控制台中都会出现错误:400(错误请求)。 Im not sure what is the mistake in the code, I will be happy if anyone helps with the issue. 我不确定代码中的错误是什么,如果有人帮助解决该问题,我将非常高兴。 Thank you. 谢谢。
Note: The URL used in the code is a dummy and not a working URL. 注意:代码中使用的URL是虚拟的,而不是有效的URL。
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.2/css/bootstrap.min.css" rel="stylesheet" type="text/css"
media="all" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/css/bootstrap-formhelpers.min.css" rel="stylesheet"
type="text/css" media="all" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/js/bootstrap-formhelpers.min.js"></script>
<script type="text/javascript">
function ok(){
var thisForm = $(this).closest('form.contactPage');
var sendData = thisForm.serialize();
jQuery.ajax({
type: "POST",
// url is dummy and not a working url
url: "dummy URL",
data: thisForm.serialize(),
data: sendData,
success : function(data, textStatus, jqXHR)
{
console.log(data);
},
error: function(jqXHR, textStatus, error){
console.log(error);
}
})
}
</script>
</head>
<body>
<form name="contactPage" class="form-email">
<input type="text" class="form-control input-lg input-sm validate-required" id="name" placeholder="Contact Person" />
<div class="form-group">
<select class="form-control input-lg bfh-countries wrapper-dropdown-2 validate-required" id="dd" data-country="US"></select>
</div>
<input type="text" class="form-control input-lg validate-required" id="contactNum" placeholder="Contact Number" />
<input type="text" class="form-control input-lg validate-required validate-email" id="email" placeholder="Email Address" />
<input type="time" class="form-group form-control input-lg validate-required" id="time" placeholder="Time" />
<input type="submit" class="form-group form-control" value="Contact" onclick="ok()" />
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.2/css/bootstrap.min.css" rel="stylesheet" type="text/css"
media="all" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/css/bootstrap-formhelpers.min.css" rel="stylesheet"
type="text/css" media="all" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-formhelpers/2.3.0/js/bootstrap-formhelpers.min.js"></script>
</head>
<body>
<form name="contactPage" id="contactPage" class="form-email">
<input type="text" class="form-control input-lg input-sm validate-required" id="name" placeholder="Contact Person" />
<div class="form-group">
<select class="form-control input-lg bfh-countries wrapper-dropdown-2 validate-required" id="dd" data-country="US"></select>
</div>
<input type="text" class="form-control input-lg validate-required" id="contactNum" placeholder="Contact Number" />
<input type="text" class="form-control input-lg validate-required validate-email" id="email" placeholder="Email Address" />
<input type="time" class="form-group form-control input-lg validate-required" id="time" placeholder="Time" />
<input type="submit" class="form-group form-control" value="Contact"/>
</form>
<script>
$(document).ready(function(){
$(function(){
$("#contactPage").submit(function(event){
event.preventDefault();
$.ajax({
url:'url.php',
type:'POST',
data:$(this).serialize(),
success:function(result){
alert(result);
}
});
});
});
});
</script>
</body>
</html>
That URL seems to invalid. 该网址似乎无效。
I suggest you check the URL given. 我建议您检查给出的URL。
The URL provided by you is not valid. 您提供的网址无效。 try again with a valid URL.. 请使用有效的网址再试一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.