2014-09-01 110 views
1
<script type="text/javascript"> 
        $('#fileUpload').submit(function (evt) { 
         evt.preventDefault(); 
         $.ajax({ 
          url: "/Transport/GridUpload", 
          type: 'POST', 
          data: $("#fileUpload").serialize(), 
          success: function (response) { 
          alert("it Works"); 
          } 
         }); 
        }); 
       </script> 

我想通過Ajax發佈表單,但它不起作用。jquery表單提交不起作用

HTML表單:

@using (Html.BeginForm("GridUpload", "Transport", new { tipas = 1 }, FormMethod.Post, new {enctype="multipart/form-data", id = "fileUpload" })) 
        { 
         <table style="margin-top:0px;"> 
          <tr> 
           <td> 
            <label for="file">Filename:</label> 
           </td> 
           <td> 
            <input type="file" name="file" id="file" /> 
           </td> 
           <td> 
            <input type="submit" class="upload-button" value="Upload" /> 
           </td> 
          </tr> 
         </table> 
        } 

我得到錯誤:

POST http://localhost:3043/Transport/GridUpload 500 (Internal Server Error) 

什麼可能是錯我的代碼?我錯過了什麼嗎? 爲了記錄,如果我刪除evt.preventDefault();一切正常,但我需要防止提交表單沒有Ajax。

+0

你不能做SUMIT和阿賈克斯一起 – 2014-09-01 10:19:44

+0

「一切正常,但我需要防止沒有Ajax提交表單。」 - 這意味着我只需要ajax提交...? 所以我應該改變$('#fileUpload')。提交什麼? – 2014-09-01 10:20:56

+0

我在你的代碼中看到你正在通過表單上傳一些數據。因此在這裏需要提交。 Ajax不起作用。 – 2014-09-01 10:22:16

回答

0

更改此

<input type="button" id="fileUpload" class="upload-button" value="Upload" /> 


$('#fileUpload').click(function (evt) { 

變化

@using (Html.BeginForm("GridUpload", "Transport", new { tipas = 1 }, 

FormMethod.Post, new {enctype="multipart/form-data", id = "fileUpload1" })) 
+0

然後如何將上傳代碼有效嗎?作爲它的上傳數據 – 2014-09-01 10:22:51

+0

@Regent好點 – Amit 2014-09-01 10:24:03

+0

爲什麼Downvote !!! – Amit 2014-09-01 10:24:19