douchongbang6011 2018-02-08 09:09
浏览 257

Ajax返回undefined

I'm trying to upload a image to my server using a POST form and AJAX. But everytime I submit the form, my AJAX returns an undefined in my error box. This is the Ajax function I'm talking about:

$('.register_submit').click(function(e){
  e.preventDefault();

  $.ajax({
      type: "POST",
      url: "./functions/register.php",
      data: new FormData(this),
      contentType: false,
      processData: false,
       success : function(data){
          if (data.code == "200"){
              window.location.replace("./?page=home");
          } else {
              $(".display-error").html("<ul>"+data.msg+"</ul>");
              $(".display-error").css("display","block");
          }
      }
  });


});

and my form:

<form class="addCar_form" data-toggle="validator"  method="post" enctype="multipart/form-data" >
   <input type="file" class="form-control productimages" id="images" name="images[]" onchange="preview_images();" multiple/>
   <input type="submit" name="addCar_submit" value="Auto toevoegen" class="btn btn-default button addCar_submit">
</form>

Please do not mark this question as a duplicate, as I've been reading about ajax and it being asynchronous in other topics but I'm really not getting it from these answers. Anyone who sees what I'm doing wrong?

  • 写回答

2条回答 默认 最新

  • dongshai1944 2018-02-08 09:40
    关注

    I think your server returns an error with a different status code. Therefor the success callback might not be called.

    There are different callback available for $.ajax I think error is the one you need to check.

    From the docs:

    error callback option is invoked, if the request fails. It receives the jqXHR, a string indicating the error type, and an exception object if applicable. Some built-in errors will provide a string as the exception object: "abort", "timeout", "No Transport".

    Try:

    $('.register_submit').click(function (e) {
        e.preventDefault();
    
        $.ajax({
            type: "POST",
            url: "./functions/register.php",
            data: new FormData(this),
            contentType: false,
            processData: false,
            success: function (data) {
                if (data.code == "200") {
                    window.location.replace("./?page=home");
                } else {
                    $(".display-error").html("<ul>" + data.msg + "</ul>");
                    $(".display-error").css("display", "block");
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
              alert(thrownError);
            }
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据