1

I'm using $.ajax to post two form select values and trigger a reinstall event, but I cannot pass the Form Data values.

This is my click.function

$('button.submitForm').click(function (event) {
     event.preventDefault()
     var form = $(this.form)
     var formCont = new mgFormControler(form.attr('id'))
     var formData = formCont.getFieldsData()

     $.ajax({
          url: form.attr('action') +
               '&loadData='+ form.attr('index') + 
               '&namespace='+ form.attr('namespace') +
               '&index='+ form.attr('index') +
               '&ajax=1' +
               '&mgformtype=' + form.attr('mgformtype'),
               type: form.attr('method'),
               processData: false,
               contentType: false,
               data: formData
          }).done(function (data) {
               console.log(form.attr('id'))
               console.log(form.attr('namespace'))
               data = data.data
               if (data.status === 'success') {
                    console.log(data.status + ': ' + data.message)
               } else {
                    console.log(data.status + ': ' + data.message)
               }
          });
     });
});

The header in dev.tools (first image) shows that formData passes as object in Query String Parameters, first image but I want to pass it as a separated element (second image)

second image

I tried new FormData() but I always get the same result with object (first image).

2 Answers 2

2

try use in your code:

    type: 'POST',
    data: jQuery.param({ field1: "hello", field2 : "hello2"}) ,
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
Sign up to request clarification or add additional context in comments.

1 Comment

I passed data: jQuery.param({ "formData[osTemplate]": "archlinux-installer_64", "formData[language]": "en"}) without contentType and it worked. Thank you @geovani-santos
0

Try JSON.stringify()

$('button.submitForm').click(function (event) {
 event.preventDefault()
 var form = $(this.form)
 var formCont = new mgFormControler(form.attr('id'))
 var formData = formCont.getFieldsData()

 $.ajax({
      url: form.attr('action') +
           '&loadData='+ form.attr('index') + 
           '&namespace='+ form.attr('namespace') +
           '&index='+ form.attr('index') +
           '&ajax=1' +
           '&mgformtype=' + form.attr('mgformtype'),
           type: form.attr('method'),
           processData: false,
           contentType: false,
           data: JSON.stringify(formData) //add this
      }).done(function (data) {
           console.log(form.attr('id'))
           console.log(form.attr('namespace'))
           data = data.data
           if (data.status === 'success') {
                console.log(data.status + ': ' + data.message)
           } else {
                console.log(data.status + ': ' + data.message)
           }
      });
 });
});

3 Comments

It passes {}.
it means formdata object is empty you can test by adding hardcoded value to formdata by using formdata[key]=value;
I just did data: jQuery.param({ "formData[osTemplate]": "archlinux-installer_64", "formData[language]": "en"}) and it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.