[英]getJSON not being called properly
I'm a beginner,so kindly help me understand this. 我是一个初学者,请帮助我理解这一点。
I have made this sample test code: (present in a external script file) 我已经制作了这个示例测试代码:(存在于外部脚本文件中)
function validateForm()
{
$.getJSON('database/grab_db.php', function(data) {
alert("hello");
return false;
});
}
which is getting called from a form: 这是从表单调用的:
<form id="form" action="users.php" method="post" onsubmit="return validateForm()">
Also,I have changed the content type to JSON
in the php file which returns the database 另外,我在返回数据库的php文件中将内容类型更改为
JSON
。
header("Content-Type: application/json");
I know for a fact that the problem lies in the validateForm()
method & the getJSON
is not getting called properly.Tried putting alert()
in validateForm()
,outside of getJSON
& it works well. 我知道问题出在
validateForm()
方法上,并且getJSON
没有正确调用。 getJSON
将alert()
放在validateForm()
,在getJSON
之外,并且效果很好。 Also,its not the case of Same Origin Policy either, as all the files are local. 同样,由于所有文件都是本地文件,因此也不是相同来源策略的情况。
Thanks in advance. 提前致谢。
Originally the return false;
最初
return false;
statement was outside of getJSON
. 语句在
getJSON
之外。 But still it was not working. 但是仍然无法正常工作。
Based on console error provided by you in the comments Uncaught ReferenceError: $ is not defined user_script.js:1 (anonymous function)
, it seems that you have not included jQuery library into the same file where this external script file is included. 基于您在注释
Uncaught ReferenceError: $ is not defined user_script.js:1 (anonymous function)
注释中提供的控制台错误,似乎您尚未将jQuery库包含在包含此外部脚本文件的同一文件中。
Please included jQuery and it should work. 请包括jQuery,它应该可以工作。
validateForm
doesn't have a return
statement (the one in the function you pass to getJSON
is a different function). validateForm
没有return
语句(传递给getJSON
的函数中的语句是另一个函数)。
This means the function returns undefined
. 这意味着该函数返回
undefined
。
This means your event handler function also returns undefined
. 这意味着您的事件处理函数也会返回
undefined
。
This doesn't stop the default action of the submit event, so the form is still submitted. 这不会停止Submit事件的默认操作,因此该表单仍被提交。
Since Ajax is asynchronous, this means the form submits before the HTTP response has come back and the callback function has run. 由于Ajax是异步的,因此这意味着表单将在HTTP响应返回和回调函数运行之前提交。
return false;
inside the callback of $.getJSON
doesn't return false for validateForm
. $.getJSON
的回调内部不会对validateForm
返回false。
You could pass the form element to validateForm
function like below: 您可以将form元素传递给
validateForm
函数,如下所示:
html: HTML:
<form id="form" action="users.php" method="post" onsubmit="return validateForm(this);">
js: JS:
function validateForm(form)
{
$.getJSON('database/grab_db.php', function(data) {
// do some checking
// if valid submit the form
form.submit();
});
// return false stop submitting the form
return false;
}
you should go with $.ajax(); 您应该使用$ .ajax(); in jquery
在jQuery中
<form id="form" action="users.php" method="post" onsubmit="return validateForm()">
Script: 脚本:
function validateForm()
{
$.ajax({
type:"post",
url: "<?php echo database/grab_db.php ?>",
cache: false,
data: $("form").serialize(),
success: function(data) // return data
{
alert(data);
}
});
return false;
}
In grab_db.php file 在grab_db.php文件中
<?php
$data = //fetch data
echo json_encode($data);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.