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.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号