Jquery 虽然在脚本中定义了Ajax函数,但无法识别它
我试图用一个按钮提交两个单独的表单。经过几天的研究,我得出了这样的结论:如果不使用AJAX,我就无法做到这一点 第一个表单提交给Mikrotik路由器(因此用户可以访问热点),该路由器检查用户名和密码。Mikrotik登录页面需要此“路径”:$(仅链接登录) 第二个表单必须发送用户输入的电子邮件,但必须在cca之后发送。0.5秒,因为用户要花那么长的时间才能访问互联网 到目前为止,我遇到了两个无法解决的错误: 第一:在这一行:Jquery 虽然在脚本中定义了Ajax函数,但无法识别它,jquery,ajax,mikrotik,Jquery,Ajax,Mikrotik,我试图用一个按钮提交两个单独的表单。经过几天的研究,我得出了这样的结论:如果不使用AJAX,我就无法做到这一点 第一个表单提交给Mikrotik路由器(因此用户可以访问热点),该路由器检查用户名和密码。Mikrotik登录页面需要此“路径”:$(仅链接登录) 第二个表单必须发送用户输入的电子邮件,但必须在cca之后发送。0.5秒,因为用户要花那么长的时间才能访问互联网 到目前为止,我遇到了两个无法解决的错误: 第一:在这一行:-未考虑引用错误:未定义SendAjax 2cnd:在这一行url:“
-未考虑引用错误:未定义SendAjax
2cnd:在这一行url:“$(仅限链接登录)”,
-未编码语法错误:意外标识符
完整代码:
!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous">
</script>
<title>Post with delay</title>
</head>
<body>
<form name="hotspot" action="$(link-login-only)" method="post" id="hotspot"
$(if chap-id) onSubmit="return doLogin(); return false;" $(endif)>
<input type="hidden" name="dst" value="$(link-orig)" />
<input type="hidden" name="popup" value="true" />
<input type="hidden" name="username" type="text" value="username" />
<input type="hidden" name="password" type="password" value="password" />
</form>
<form name="mail" action="http://myserveraddress/verifysanitize.php" method="post" id="mail">
<h1>Hotspot</h1>
<h2>To gain internet access, enter your email.</h2>
<br />
<input type="text" name="email" autofocus="autofocus">
<br />
<input type="button" value="Submit" id="submit" onclick="SendAjax()"> <br />
</form>
</body>
<script rel="javascript" type="text/javascript">
function SendAjax() {
var email = $("#email").val();
// Check if fields are empty
if (email=="") {
alert("Please enter your email.");
}
// AJAX code to submit form
else {$.ajax({
type: "POST"
url: "$(link-login-only)",
data: $("#hotspot").serialize(),
});
function callback(){
$.ajax ({
url: $("#mail").attr('action'),
}
});
}
setTimeout(callback(), 1000);
}
}
</script>
</html>
!DOCTYPE html>
延迟投递
热点
要访问internet,请输入您的电子邮件。
函数SendAjax(){
var email=$(“#email”).val();
//检查字段是否为空
如果(电子邮件==“”){
提醒(“请输入您的电子邮件”);
}
//提交表单的AJAX代码
else{$.ajax({
类型:“职位”
url:“$(仅限链接登录)”,
数据:$(“#热点”).serialize(),
});
函数回调(){
$.ajax({
url:$(“#邮件”).attr('action'),
}
});
}
setTimeout(callback(),1000);
}
}
在发布前查看代码后,在第一个表单上添加一个隐藏按钮,并使用ajax发送第一个表单,然后延迟0.5秒,然后执行第二个表单,这是一个解决方案吗?请尝试以下操作:
!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous">
</script>
<title>Post with delay</title>
</head>
<body>
<form name="hotspot" action="$(link-login-only)" method="post" id="hotspot"
$(if chap-id) onSubmit="return doLogin(); return false;" $(endif)>
<input type="hidden" id="dst" name="dst" value="$(link-orig)" />
<input type="hidden" id="popup" name="popup" value="true" />
<input type="hidden" id="username" name="username" type="text" value="username" />
<input type="hidden" id="password" name="password" type="password" value="password" />
</form>
<form name="mail" action="http://myserveraddress/verifysanitize.php" method="post" id="mail">
<h1>Hotspot</h1>
<h2>To gain internet access, enter your email.</h2>
<br />
<input type="text" id="email" name="email" autofocus="autofocus">
<br />
<input type="button" value="Submit" id="submit" onclick="SendAjax()"> <br />
</form>
</body>
<script rel="javascript" type="text/javascript">
$(document).on('click', '#submit', function(event) {
if (!$("#email").val()) {
alert("Please enter your email.");
return false;
}
// AJAX code to submit form #hotspot
$.ajax({
type: "POST"
url: "$(link-login-only)",
data: $("#hotspot").serialize(),
success: (data => {
console.log(data)
// After getting response of #hotspot, Subbmitting the second form #mail
$.ajax ({
url: $("#mail").attr('action'),
data: $("#mail").serialize(),
success: (dataMail => {
console.log(dataMail);
}),
error: (errorMail => {
console.log(`#hotspot successfuly submitted but #mail getting error: ${errorMail}`)
})
})
}),
error: (error => {
console.log(`#hotspot not submitted because #mail getting error: ${error}`);
})
});
});
</script>
</html>
!DOCTYPE html>
延迟投递
热点
要访问internet,请输入您的电子邮件。
$(文档)。在('单击','提交')功能(事件){
如果(!$(“#电子邮件”).val()){
提醒(“请输入您的电子邮件”);
返回false;
}
//提交表单#热点的AJAX代码
$.ajax({
类型:“职位”
url:“$(仅限链接登录)”,
数据:$(“#热点”).serialize(),
成功:(数据=>{
console.log(数据)
//收到#热点的响应后,再提交第二张表单#邮件
$.ajax({
url:$(“#邮件”).attr('action'),
数据:$(“#邮件”).serialize(),
成功:(数据邮件=>{
console.log(数据邮件);
}),
错误:(errorMail=>{
log(`hotspot已成功提交,但`mail get error:${errorMail}`)
})
})
}),
错误:(错误=>{
log(`hotspot未提交,因为`mail get error:${error}`);
})
});
});
请尝试以下操作:
!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous">
</script>
<title>Post with delay</title>
</head>
<body>
<form name="hotspot" action="$(link-login-only)" method="post" id="hotspot"
$(if chap-id) onSubmit="return doLogin(); return false;" $(endif)>
<input type="hidden" id="dst" name="dst" value="$(link-orig)" />
<input type="hidden" id="popup" name="popup" value="true" />
<input type="hidden" id="username" name="username" type="text" value="username" />
<input type="hidden" id="password" name="password" type="password" value="password" />
</form>
<form name="mail" action="http://myserveraddress/verifysanitize.php" method="post" id="mail">
<h1>Hotspot</h1>
<h2>To gain internet access, enter your email.</h2>
<br />
<input type="text" id="email" name="email" autofocus="autofocus">
<br />
<input type="button" value="Submit" id="submit" onclick="SendAjax()"> <br />
</form>
</body>
<script rel="javascript" type="text/javascript">
$(document).on('click', '#submit', function(event) {
if (!$("#email").val()) {
alert("Please enter your email.");
return false;
}
// AJAX code to submit form #hotspot
$.ajax({
type: "POST"
url: "$(link-login-only)",
data: $("#hotspot").serialize(),
success: (data => {
console.log(data)
// After getting response of #hotspot, Subbmitting the second form #mail
$.ajax ({
url: $("#mail").attr('action'),
data: $("#mail").serialize(),
success: (dataMail => {
console.log(dataMail);
}),
error: (errorMail => {
console.log(`#hotspot successfuly submitted but #mail getting error: ${errorMail}`)
})
})
}),
error: (error => {
console.log(`#hotspot not submitted because #mail getting error: ${error}`);
})
});
});
</script>
</html>
!DOCTYPE html>
延迟投递
热点
要访问internet,请输入您的电子邮件。
$(文档)。在('单击','提交')功能(事件){
如果(!$(“#电子邮件”).val()){
提醒(“请输入您的电子邮件”);
返回false;
}
//提交表单#热点的AJAX代码
$.ajax({
类型:“职位”
url:“$(仅限链接登录)”,
数据:$(“#热点”).serialize(),
成功:(数据=>{
console.log(数据)
//收到#热点的响应后,再提交第二张表单#邮件
$.ajax({
url:$(“#邮件”).attr('action'),
数据:$(“#邮件”).serialize(),
成功:(数据邮件=>{
console.log(数据邮件);
}),
错误:(errorMail=>{
log(`hotspot已成功提交,但`mail get error:${errorMail}`)
})
})
}),
错误:(错误=>{
log(`hotspot未提交,因为`mail get error:${error}`);
})
});
});
我觉得你的代码很好。你有语法错误,请检查下面的代码
对于第一个错误,您需要调用button事件上的函数
$(document).on('click', '#submit', function(event) {
event.preventDefault();
var email = $("#email").val();
// Check if fields are empty
if (email=="") {
alert("Please enter your email.");
}else {
$.ajax({
type: "POST"
url: "$(link-login-only)",
data: $("#hotspot").serialize(),
})
.always(function() {
console.log("first call completed");
});
function callback(){
$.ajax ({
url: $("#mail").attr('action'),
// } you need to remove this parenthesis
});
}
setTimeout(callback(), 1000);
}
});
第二个错误是由于您有一个额外的右括号
}
您的代码对我来说似乎很好您有语法错误请检查下面的代码
对于第一个错误,您需要调用button事件上的函数
$(document).on('click', '#submit', function(event) {
event.preventDefault();
var email = $("#email").val();
// Check if fields are empty
if (email=="") {
alert("Please enter your email.");
}else {
$.ajax({
type: "POST"
url: "$(link-login-only)",
data: $("#hotspot").serialize(),
})
.always(function() {
console.log("first call completed");
});
function callback(){
$.ajax ({
url: $("#mail").attr('action'),
// } you need to remove this parenthesis
});
}
setTimeout(callback(), 1000);
}
});
第二个错误是由于您有一个额外的右括号
}
未定义SendAjax的原因是您的脚本可能与代码不在同一文件中。SendAjax的工作原理如下:
延迟投递
热点
要访问internet,请输入您的电子邮件。
函数SendAjax(){
警报(“正常”);
}
未定义SendAjax的原因是脚本可能与代码不在同一文件中。SendAjax的工作原理如下:
延迟投递
热点
要访问internet,请输入您的电子邮件。
函数SendAjax(){
警报(“正常”);
}