0

First of all sorry for my English I am trying to send an ajax request. I am using FormData. But when I append data then console.log that format it says no properties. FormData constructor accepts form as a parameter if i am not wrong. Here I used that argument, but also when i use formdata.append(key, value) this is not working also

Here is my code

(No Jquery used $.ajax is my self written library).

onValidated: function(form){
    var formData = new FormData(form);

    console.log(formData);

    $.ajax({
        url: '/comment/send',
        type: 'POST',
        contentType: 'application/json',
        dataContent: formData,

        start: function()
        {
            $preloader.show();
        },

        success: function(response)
        {
            $preloader.hide();
        },

        error: function(response)
        {
            return false;
        }
    });
}

And here is my $.ajax function

window.$.ajax = function(settings)
{
    var request;

    if (window.XMLHttpRequest) 
    {
        request = new XMLHttpRequest();
    }
    else{
        request = new ActiveXObject("Microsoft.XMLHTTP")
    }

    request.open(settings.type, settings.url, true);
    request.setRequestHeader('Content-Type', settings.contentType);

    request.onload = function() {
      if (request.status >= 200 && request.status < 400) {
        settings.success(JSON.parse(request.responseText));
      }
    };

    request.onerror = function() {
      settings.error();
    }

    console.log(settings.dataContent);

    // Used for preloader or something else
    settings.start();


    if (settings.type == 'POST') 
    {
        request.send(settings.dataContent); 
    }
    else if(settings.type == 'GET')
    {
        request.send();
    }
    else
    {
        return false;
    }
}       
  • Correct me if I'm wrong but last time I had to use `formData` I couldn't see what was actually appended. What if you check in the actual network request, do you see anything there? – Chrillewoodz Oct 13 '16 at 05:59
  • Server gives me 500 thats why i can't see the server log error I think its because I'm sending empty FormData – Sanjar Mirakhmedov Oct 13 '16 at 06:02
  • Hmm, you probably want to check if that is actually the case. Because the issue could lie somewhere on the server side. – Chrillewoodz Oct 13 '16 at 06:04
  • No server side is ok cause when i send just simple {} js object as data it returns correct response and logs it on server i thing problem is on client side – Sanjar Mirakhmedov Oct 13 '16 at 06:07
  • "Server gives me 500 thats why i can't see the server log error" — That doesn't make sense. A 500 is an instruction that you should look at the error log. – Quentin Oct 13 '16 at 09:41
  • @SanjarMirakhmedov — "No server side is ok cause when i send just simple {} js object as data it returns correct response" — Well, you're not doing that any more are you? You're sending FormData. Have you looked at the request body you are sending using the Network tab of your browser's developer tools? Have you made sure the server can handle that? – Quentin Oct 13 '16 at 09:41

0 Answers0