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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)