2

Actually I'm trying to upload a image on server using ajax jquery?

My Code:-

$(function(){ $('#uploadFile').submit(function(){ var formData = new FormData(); formData.append('file', $('#file')[0].files[0]); $.ajax({ url : 'upload.php', type : 'POST', data : formData, success : function(data) { console.log(data); alert(data); } }); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="uploadFile"> <input type="file" id="file" /> <button type="submit">Submit</button> </form>

Thankyou for your efforts!

2

1 Answer 1

2

$(function(){ $('#uploadFile').submit(function(){ var formData = new FormData(); formData.append('file', $('#file')[0].files[0]); $.ajax({ url : 'upload.php', type : 'POST', data : formData, contentType: false, // You need to add this processData: false,// You need to add this also success : function(data) { console.log(data); alert(data); } }); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="uploadFile"> <input type="file" id="file" /> <button type="submit">Submit</button> </form>

Sign up to request clarification or add additional context in comments.

1 Comment

Now Its working fine... Thanks @Hakim Singh

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.