简体   繁体   English

Ajax 文件上传后没有得到成功响应

[英]Ajax doesn't get a success response back after file upload

I just started with Ajax and also tried to find a solution for this.我刚开始使用 Ajax 并尝试为此找到解决方案。

Here is the problem:这是问题所在:

I upload a.csv to a server.我将 a.csv 上传到服务器。 This works just fine.这很好用。 But after the upload "success" in the ajax call won't respond.但是在ajax调用上传“成功”后就没有响应了。 Neither does complete or error.既不完整也不错误。 It shows nothing.它什么也没显示。 Not even an empty alert.甚至没有空警报。 Just nothing.根本不值一提。 I also didn't find anything in the logs.我也没有在日志中找到任何东西。

When I click the upload button without chosing a file to upload I get a response from success...当我在没有选择要上传的文件的情况下单击上传按钮时,我得到成功的响应...

Here is the code:这是代码:

upload.php上传.php

<?php
header('Content-type: application/json');
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$response[] ='';

  if(move_uploaded_file($_FILES["file"]["tmp_name"],$target_file))
  { 
    //$response['message'] = "File was uploaded!";
    $response['message'] = csvToJson($target_file);
  }
  else
  { 
    $response['message'] = 'Sorry, there was an error uploading your file.'; 
  }

echo json_encode($response);


function csvToJson($file) 
{
    // open csv file
    if (!($fp = fopen($file, 'r'))) {
        die("Can't open file...");
    }
    
    //read csv headers
    $key = fgetcsv($fp,"1024",";");
    
    // parse csv rows into array
    $json = array();
        while ($row = fgetcsv($fp,"1024",";")) {
        $json[] = array_combine($key, $row);
    }
    
    // release file handle
    fclose($fp);
    
    // encode array to json
    return $json;
}
?>

upload.js上传.js

$(document).ready(function(e)
{
    // Submit form data via Ajax
    $("#form").on('submit', function(e)
    {
        e.preventDefault();
        var form_data = new FormData($(this)[0]);  
        $.ajax({    
            type: 'POST',
            url: 'upload.php',
            data: form_data,
            contentType: false,
            cache: false,
            processData:false,
            success: function(response)
            { 
                //var json = $.parseJSON(response)
                alert(response);
            },
            error: function(error){
                console.log("Error:");
                console.log(error);
           }, 
           complete: function(response){
            alert(response);
           }

        });
    });
});

the form in index.php index.php 中的表格

<form id=form enctype="multipart/form-data">
        <input type="file" name="file" id="file">
        <input type="submit" value="Upload Excel" name="submit">
</form>

In the end I want a json back with the content of the uploaded file.最后我想要一个 json 返回上传文件的内容。 But right now there is zero output if the file is uploaded.但是现在,如果文件已上传,则 output 为零。

Thanks for any advice!感谢您的任何建议!

EDIT for Solution:编辑解决方案:

The problem was something way different.问题完全不同。 I had to format the output from csvToJson to UTF-8. After that I get a json as the respone.我必须将 output 从 csvToJson 格式化为 UTF-8。之后我得到一个 json 作为响应。

Function for formatting to UTF-8 (got it from another post here) Function 用于格式化为 UTF-8(从此处的另一篇文章中获取)

function utf8ize($d) {
  if (is_array($d)) {
      foreach ($d as $k => $v) {
          $d[$k] = utf8ize($v);
      }
  } else if (is_string ($d)) {
      return utf8_encode($d);
  }
  return $d;
}

Solution, but cannot mark it:解决方案,但无法标记:

The problem was something way different.问题完全不同。 I had to format the output from csvToJson() in upload.php to UTF-8. After that I get a json as the respone.我必须将 output 从 upload.php 中的 csvToJson() 格式化为 UTF-8。之后我得到一个 json 作为响应。

Function for formatting to UTF-8 (got it from another post here) Function 用于格式化为 UTF-8(从此处的另一篇文章中获取)

function utf8ize($d) {
  if (is_array($d)) {
      foreach ($d as $k => $v) {
          $d[$k] = utf8ize($v);
      }
  } else if (is_string ($d)) {
      return utf8_encode($d);
  }
  return $d;
}

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

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