drngnh708353 2015-12-30 11:32
浏览 33
已采纳

为什么控件没有从PHP文件返回到AJAX成功函数?

I'm using PHP, Javascript, AJAX for my website.

I'm putting below only the necessary code.

JS code(AJAX function code):

$("#btn_add_event").click(function(){

    var strSeriaze = $( "#formAddEvent" ).serialize();
    url = $( "#formAddEvent" ).attr('action');
    $("#btn_add_event").attr('disabled', 'disabled');
    $("#addEventErrorMsg").html('');
    $.ajax({
        url: url,
        type: "POST",
        data:  {postData:strSeriaze},
        beforeSend: function(){
            $('#loader-icon').show();
        },
        complete: function(){
            $('#loader-icon').hide();
        },
        success: function(data){
        //Control is not returning here on success from PHP file
            $('#loader-icon').hide();

            if(data == "Success")
            {
                $("#myModal-add-event").modal('hide');
                $("#myModal-add-event").hide();
                window.location.href = site_url + "event_index.php";
                return false;
            }
            else
            {
                $("#btn_add_event").attr('disabled', false);
                $("#addEventErrorMsg").show();
                $("#addEventErrorMsg").html(data);
            }
        },
        error: function(){}
    });   
  }) 

I tried to debug the issue the call is going perfectly to PHP file, the logic written over there is also working but the control is not getting returned to the js file(i.e. AJAX success function). In turn I'm unable to execute code written inside success function.

Please correct the mistake I'm making in my code.

Following is the necessary PHP code after execution it is expected to return the control to js file(i.e. AJAX success funtion) but it's not happening.

if($ret) {
  $eventAddResultArr = $objEvent->GetResponse();

  if($eventAddResultArr['msg'] == 'Success') {
    echo "Success";
    exit;
  //From here it is expected to return the control to js file's AJAX success function but it's stopping execution here only
  } else {
    $errMsg = "";
    foreach($eventAddResultArr['msg'] as $key => $err_msg) {
      $errMsg .= $err_msg."<br>";
    }       
    echo $errMsg;
    exit;
  }
}

Note : I've put in comments in my code to make you understand my issue better. Still if you need any further information regarding the issue I'm facing please do let me know.

Thanks.

  • 写回答

2条回答 默认 最新

  • duanjiuhong5843 2015-12-30 11:52
    关注

    If you are not using DataType in ajax than you can follow this:

    In php

    Use echo true; instead of echo "Success";

    And in Ajax, check with:

    if(data == true) {
    //your code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真