Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload a file using jquery's $.ajax function with json and php

I am trying to upload a file using jQuery's $.ajax function but didn't get any output. Somebody please help me to solve this. I don't know if this script is correct. My script is:

$.ajax({
  url:'newsup.php',
  data: "",
  type: 'POST',
  contentType:'multipart/form-data',
  dataType: 'json',
  catche: 'false',

  success:function(data6)
  {
    $("#disp").removeClass().addClass((data6.error=== false)? 'success':'error').html(data6.msg).fadeIn('fast');
    //dele();
    if($("#disp").hasClass('success'))
    {
      alert("success");
      setTimeout("$('#disp').fadeOut('slow')",3000);            
    }
  },

  error:function(XMLHttpRequest,textStatus,errorThrown)
  {
    $("#disp").removeClass().addClass('error').html("There was an <strong>"+errorThrown+"</strong> error due to  <strong>"+textStatus+" condition").fadeIn('fast');
  }              

});

Also I need help getting data from file uploading field using jQuery.

like image 817
JOE Avatar asked Nov 20 '25 18:11

JOE


1 Answers

Please use plugin for this.
In my opinion this plugin is better solution for this.You don't need to remember all options etc.Just replace your 'ajax' to 'ajaxForm'.

Please read examples ,below
http://jquery.malsup.com/form/#ajaxForm

like image 74
Oyeme Avatar answered Nov 23 '25 09:11

Oyeme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!