1

i'm trying to make a simple form to upload files with jquery/ajax. this is a part of my code:

  var formData = new FormData($(this)[0]);

  $.ajax({
    url: 'uploader.php',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (data) {
      $('#ShowR').html(data);
    }
  });

i'm trying to change this code to $.post method like this:

var formData = new FormData($(this)[0]);

$.post('uploader.php', {action:"ShowGTR",MyFiles:formData},
function(data) {
 $('#ShowR').html(data);
});

i tried some ways but i couldn't fix the code and in Google Chrome console i got this error:

Uncaught TypeError: Illegal invocation 

so i need your help to fix this code and convert $.ajax method to $.post method. i really appreciate if anyone can help me for this.

Alireza
  • 1,048
  • 5
  • 18
  • 36
  • Are you trying to convert the $.ajax to $.post because you want to add a parameter to the request? – Musa May 26 '14 at 00:41

2 Answers2

1

If the only reason you want to convert your $.ajax call to a $.post call is to add a parameter, then you don't need to convert it ro $.post. What you have to do is append the parameter to the formdata object.

  var formData = new FormData(this);
  formData.append("action", "ShowGTR");

  $.ajax({
    url: 'uploader.php',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (data) {
      $('#ShowR').html(data);
    }
  });
Musa
  • 96,336
  • 17
  • 118
  • 137
-3
$.post( "ajax_file.php", {username:"someone"} ,function( data ) {
    $("#div-id").html(data);
});

This is general syntax of jquery post.

  • i know it! but my question is something else! how i can convert the ajax method (first code) to the post method (second code) :( – Alireza May 25 '14 at 17:19
  • Try `var formData = new FormData($(this)[0]); console.log(formData);` What you see in console? – user3435674 May 25 '14 at 17:26
  • this is a new error: Uncaught ReferenceError: formData is not defined – Alireza May 25 '14 at 17:28
  • So, it's error here. I don't know it but it's reference error. this what is this ? Value of this changes so it may causes error... – user3435674 May 25 '14 at 17:32
  • my dear friend, i think you didn't read my question! the first code is correct and working very well. but i need to use post method beside ajax! that's it! but thank you for answers. – Alireza May 25 '14 at 17:34
  • http://stackoverflow.com/questions/10324594/jquery-illegal-invocation this may help you – user3435674 May 25 '14 at 17:34
  • try this, `var obj = {data: formData); $.post( "uploader.php", obj ,function( data ) { $('#ShowR').html(data); });` it's exact convert from ajax to post in jquery – user3435674 May 25 '14 at 17:38
  • I read your question and i am trying to answer you. Even i don't know exact answer. But I am trying to guess it. Anyway, I am going off now. hope you will solve it soon. bye – user3435674 May 25 '14 at 17:43