粥虚尘 2015-06-23 08:00 采纳率: 0%
浏览 4659

使用$.post,数据已提交,但回调函数不执行

我用$.post希望实现异步提交的功能,代码如下:

 $.post(addURL,
        {pid : id},
        function (data) {
            alert("回调函数");
            alert(data["status"]);
        },
        "json")
        .success(function () {alert("success");})
        .error(function () {alert("error");}
);

PHP代码

$db = M('cart');
$pid = I('pid', 0, 'intval');
$cart = array(
    'aid' => (int) $_SESSION['uid'],
    'pid' => $pid,
    'num' => 1
    );
$return = array();
// 检查购物车中是否已有相同商品
if ($db->where($cart)->count()) {
    $return['status'] = 0;
} else {
    $db->add($cart);
    $return['status'] = 1;
}

// ajaxReturn()为ThinkPHP框架提供的方法
$this->ajaxReturn($return, 'json');

提交之后数据被插入了数据库,但是回调函数并没有执行,并且alert了error信息。但是我如果使用$.ajax方法却能够正确执行回调函数。

 $.ajax({
    url : addURL,
    async : false,
    type : "POST",
    data : {pid : id},
    dataType : "json",
    timeout : 1000,
    error : function () {alert("error");},
    success : function (data) {
        alert("回调函数");
        alert(data.status);
    }
});

不过,虽然$.ajax提交成功并且正确执行了回调函数,但是在开发人员工具中并不能看到我php控制器的响应(之前用$.post方法提交的时候能够看到控制器的响应)。

对于$.ajax和$.post这种傲娇的行为我实在是不能理解……

  • 写回答

2条回答 默认 最新

  • cy_Calvin 2015-06-24 07:14
    关注
        .success(function () {alert("success");})
        .error(function () {alert("error");} 
                这是干什么的,他不在$.post里的吧,你去掉试试
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿