即使在请求数据之后,还没有在jquery中调用完成的函数
AJAX发布请求:({"name":"name","fname":"testing","ques":"question","sec":"answer"})
来自php的回复:({"invalid":"error"})
,
即正在执行其他部分&不满足if条件。
jQuery代码:
$("#askques").on("submit", function(event) {
event.preventDefault();
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
var formdata=JSON.stringify(($('#askques').serializeObject()));
$.ajax({
type: 'POST',
contentType: "Content-Type: application/json",
url: 'ajaxphp/askques.php',
data: formdata,
dataType: 'json',
cache: false
}).done(function(data) {
if ('url' in data) {
$("#asknext").load("quesans/form.htm", function () {
$("#askcon").hide();
});
}
else if('prob' in data)
{
$("#resposne").text("Something went wrong, try again.");
}
else if('invalid' in data)
{
$("#resposne").text("Enter just the First Name, a word with letters only !");
}
});
});
PHP代码:
$name = $_POST["name"];
$fname = $_POST["fname"];
$ques = $_POST["sec"];
$ans = $_POST["ans"];
$time=date("y-m-d H:i:s");
if (ctype_alpha($name) && ctype_alpha($fname) && (strlen($ques)<=512) && (strlen($ans<=1024)) && (strlen($ques)>3) && (strlen($ans)>3) )
{
// not executed
}
else
{
$data = array('invalid' => "error" );
echo json_encode($data);
}
即使在请求数据后,也没有在jquery中调用完成的函数&amp;得到了来自php文件的响应,但是其他部分正在执行&amp;不满足if条件。
答案 0 :(得分:1)
问题在于您检查data
对象中的属性的方式。像这样更改你的done()
它应该有效:)
done(function(data) {
if (data.url) {
$("#asknext").load("quesans/form.htm", function () {
$("#askcon").hide();
});
}
else if(data.prob){
$("#resposne").text("Something went wrong, try again.");
}
else if(data.invalid){
$("#resposne").text("Enter just the First Name, a word with letters only !");
}
});
答案 1 :(得分:1)
contentType: "Content-Type: application/json",
删除上面的代码行,在使用它时你没有在你的php
中获得$ _POST变量$.ajax({
type: 'POST',
//contentType: "Content-Type: application/json",
url: 'ajaxphp/askques.php',
data:{"name":"name","fname":"testing","ques":"question","sec":"answer"},
dataType: 'json',
cache: false
}).done(function(data) {
console.log(data);
if ('url' in data) {
$("#asknext").load("quesans/form.htm", function () {
$("#askcon").hide();
});
}
else if('prob' in data)
{ alert("Entered Prob")
$("#resposne").text("Something went wrong, try again.");
}
else if('invalid' in data)
{
alert("Entered Invalid")
$("#resposne").text("Enter just the First Name, a word with letters only !");
}
});
在你的php文件中放置
//print_r($_POST);
$name = $_POST["name"];
$fname = $_POST["fname"];
$ques = $_POST["sec"];
$ans = $_POST["ans"];
$time=date("y-m-d H:i:s");
if (ctype_alpha($name) && ctype_alpha($fname) && (strlen($ques)<=512) && (strlen($ans<=1024)) && (strlen($ques)>3) && (strlen($ans)>3) )
{
$data = array('prob' => "error" );
echo json_encode($data);
}
else
{
$data = array('invalid' => "error" );
echo json_encode($data);
}
在您输入上述代码后,请尝试查看您是否收到任何提醒。
如果它没有警告然后取消注释我给出的php代码的第一行并检查控制台响应,你会看到一个变量数组,如果它发布,否则你看到空数组