dongxia9519 2017-04-04 18:34
浏览 118
已采纳

为什么我不能使用jquery发布到laravel路由?

I'm trying to submit a single value as following?

HTML:

 <form class="form-horizontal" role="form" method="POST"  enctype="multipart/form-data" name="frmanalyse" id="frmanalyse">
{{ csrf_field() }}
 <label for="marginsource" style="float: left;  width:150px; text-align:left;">Margin Source</label>
 <input type="file" name="marginsource" id="marginsource" >
 <br />
 </form>

script:

 <script type="text/javascript">
  $( "#frmanalyse" ).submit(function(event) {
            $.post( "marginanalyser", {username: "medo ampir"}, function( data ) {
                alert(data);
            });
            event.preventDefault();
  });

in laravel routes:

Route::post('marginanalyser',function(Request $request){
 echo $request->input('username');

 $file = $request->file('marginsource');
echo 'File Name: '.$file->getClientOriginalName();
});

nothing shows in the message at all.

  • 写回答

1条回答 默认 最新

  • douliang9057 2017-04-04 19:04
    关注

    Change your JavaScript to use FormData as you aren't submitting the file

    $( "#frmanalyse" ).submit(function(event) {
        event.preventDefault();
    
        var formData = new FormData();
        formData.append('marginsource', $('#marginsource')[0].files[0]);
        formData.append('username', "medo ampir");
    
        $.ajax({
            url : window.location.origin + "/marginanalyser",
            type: "POST",
            data : formData,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            processData: false,
            contentType: false,
            success:function(data, textStatus, jqXHR) {
                console.log(data);
            },
            error: function(jqXHR, textStatus, errorThrown){
                //if fails     
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?