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条)

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用