【发布时间】:2012-01-30 11:41:01
【问题描述】:
我正在使用 Codeigniter MVC 框架并且我正在努力使用 Jquery Ajax。 我的问题是如何在 ajax 成功函数中放置条件语句?
基本上我想要做的是在成功输入后它会重定向到另一个页面,如果没有会显示错误:手机号码不匹配或为空请填写空白。
控制器方法
if($mobile_number == "") {
$data = array('message' => "Please fill up the blanks", 'success' => 'no');
}
elseif($mobile_number != "123") {
$data = array('message' => "Please does not match", 'success' => 'no');
}else {
#redirect('home/test');
$data = array('message' => "Mobile match.", 'success' => 'yes');
}
$output = json_encode($data);
echo $output;
my_ajax.js
$(document).ready(function(){
$("#request_submit").click(
function(){
var mobtel=$("#mobtel").val();
$.ajax({
type: "POST",
url: "post_action",
dataType: "json",
data: "mob_tel="+mobtel,
cache:false,
success: function (data) {
if(data.success == 'yes') {
$("#form_message").html(data.message).fadeIn('slow');
}
else if(data.success == 'no') {
alert('error');
}
}
});
});
});
提前致谢。希望有人可以在这里帮助我。 (T_T)
【问题讨论】:
-
我认为你不应该修剪
data,因为它已经是一个解析对象而不是字符串。 -
此外,您的成功函数中的第二个
if语句应该是else if语句,因为data.success不能同时是'yes'和'no' -
更新了我的代码。还是不行。您可以查看我的网站:apps.stratpoint.com:9114/home/request_verification
-
尝试在 echo $output 后添加退出;在你的 php 代码中
-
使用我的jsfiddle example function在控制台登录数据对象的结构,查看服务器实际返回的是什么。
标签: jquery ajax codeigniter