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);
            }
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮