dongruyan4948 2015-07-14 07:22
浏览 45

Javascript文件提交导致PHP脚本中的服务器错误

I've been going mad this past month trying to build a page to submit a pdf asynchronously to our server. I'm an experienced programmer, but never worked in web-dev or in php or javascript.

The javascript submission function is as follows:

function sendFile(event)
{
    $("#submitForm").hide();
    var formData = new FormData();
    var file=$("#journal")[0].files[0];
    formData.append("document", file);
    $.ajax("converter.php", { data:formData, processData:false, dataType:"text", method:"post", success:function(data, textStatus, jqXHR){
        if(data.length == 8)
        {
            $("#key_input").val(data);
            $("#key_submission").submit();
        }
        else
        {
            $("#response").html(data);
        }
    } });

It submits to the server but as soon the php script reaches this line

if(isset($_POST['document']))

the program crashes and I get an internal server error. What could be causing the crash?

  • 写回答

1条回答 默认 最新

  • drqvr26084 2015-07-14 07:37
    关注

    This should be done with $.ajax() this way:

    $.ajax({ //<------"{" an object should be passed here so you are missing a "{"
       url:"converter.php", //<-----"url:" was missing.
       data:formData, 
       contentType:false, //<-----this is also needed for file upload
       processData:false, 
       dataType:"text", 
       type:"post", //<------should be type not method 
       success:function(data, textStatus, jqXHR){
         if(data.length == 8){
                $("#key_input").val(data);
                $("#key_submission").submit();
         }else{
                $("#response").html(data);
         }
       } 
    });
    

    If you are trying to upload a file then contentType:false is needed with processData:false.

    FormData
    You can read more here.

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容