There is a form with code:

$j('.save').click(function (e) { var cont = $j('.edit').serialize(); $j.ajax({ type: 'POST', url: '/admin/edit.php', data: cont, success: function(data) { alert('Страница успешно изменена'); }, error: function(xhr, str){ alert('Возникла ошибка: ' + xhr.responseCode); } }); }); 

How to make the files sent too?

    1 answer 1

    Greetings

    You will not send files this way. Serialize does not allow you to send a file, you need to use FormData. Of course, you won't be able to execute code in the proposed stackoverflow functionality, but you can copy yourself =)

     $(documen).ready(function(){ $("form[name=SendFileForm]").submit(function(){ var formData = new FormData($('form[name=SendFileForm]')[0]); $.ajax({ url: '/', //куда отправить данные type: 'POST', data: formData, cache: false, dataType: 'json', processData: false, contentType: false, success: function( respond ){ // ожидаем json в формате {"success":true|false,"message":"сообщение"} if( respond.success ){ alert(respond.message); // Файл загружен } else{ alert( respond.message ); // Ошибки на стороне сервера } } }); return false; }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form name="SendFileForm"> <input type="file" name="myfile"> <button type="submit">send file</button> </form> 

    • It would be nice to mention that FormData is only for IE10 + - kroder
    • =) Yes, everything is fine, now most of the browsers support the basic data. In extreme cases, you can take jquery.form. A good example of hayageek.com/ajax-file-upload-jquery - Stanislav