简体   繁体   English

Ajax返回未定义

[英]Ajax returns undefined

I'm trying to upload a image to my server using a POST form and AJAX. 我正在尝试使用POST表单和AJAX将图像上传到服务器。 But everytime I submit the form, my AJAX returns an undefined in my error box. 但是每次我提交表单时,我的AJAX都会在错误框中返回一个undefined This is the Ajax function I'm talking about: 这是我正在谈论的Ajax函数:

$('.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. 请不要将此问题标记为重复问题,因为我一直在阅读有关ajax的信息,而该问题在其他主题中是异步的,但是我确实没有从这些答案中得到答案。 Anyone who sees what I'm doing wrong? 有人看到我在做什么错吗?

The click handler for $(".register_submit") would be targeting the button; $(".register_submit")的点击处理程序将定位按钮; then the button is passed in through FormData(this) . 然后通过FormData(this)传递按钮。 FormData should instead contain a reference to the form... FormData应该包含对表单的引用。

Also, I don't see a register_submit class definition on the form in this example, so maybe it's getting overlooked? 另外,在此示例中,我在窗体上看不到register_submit类定义,所以也许它被忽略了吗?

In your PHP page, can you trace through it to see what form data is being passed? 在您的PHP页面中,您可以跟踪它以查看正在传递的表单数据吗? That is helpful to track down these kinds of problems, by evaluating the form posted data. 通过评估表单发布的数据,这有助于查找此类问题。

I think your server returns an error with a different status code. 我认为您的服务器返回的错误带有不同的状态代码。 Therefor the success callback might not be called. 因此,可能不会调用success回调。

There are different callback available for $.ajax I think error is the one you need to check. $.ajax有不同的回调可用,我认为error是您需要检查的。

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. 它接收jqXHR,指示错误类型的字符串以及一个异常对象(如果适用)。 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);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM