如何使用jquery的$ .ajax函数和json和php上传文件

JOE*_*JOE 1 php ajax jquery json

我试图使用jQuery的$.ajax函数上传文件,但没有得到任何输出.有人请帮我解决这个问题.我不知道这个脚本是否正确.我的脚本是:

$.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');
  }              

});
Run Code Online (Sandbox Code Playgroud)

我还需要帮助使用jQuery从文件上传字段中获取数据.

Oye*_*eme 6

请使用插件.
在我看来这个插件是更好的解决方案.你不需要记住所有选项等.只需将你的'ajax'替换为'ajaxForm'.

请阅读http://jquery.malsup.com/form/#ajaxForm下面的示例