signalpoint / DrupalGap

An application development kit for Drupal websites.
https://www.drupalgap.org
GNU General Public License v2.0
232 stars 185 forks source link

Invalid json data on services.call #1012

Closed jsbriantes closed 6 years ago

jsbriantes commented 6 years ago

Hi. Following the latest suggestion on #831, I have installed Services Contact module and I create my own form to send contact messages. This is my submit function:

function mymodule_mycontact_form_submit(form, form_state) {
  try {     
    var myformdata = {
        name: form_state.values['name'],
        mail: form_state.values['email'],
        subject: form_state.values['subject'],
        message: form_state.values['message'],
        cid: 1
    };   

    Drupal.services.call({
        method: 'POST',
        path: 'contact/site',
        data: myformdata,
        contentType: 'application/json',
        success: function(result) {
          console.log(result);
          var msg = 'Your message has been sent.';
          drupalgap_alert(msg);
        },
        error: function(result) {
            console.log(result);
            var msg = 'Error sending your message';
            drupalgap_alert(msg);
        }
    });
  }
  catch (error) { console.log('mymodule_mycontact_form_submit - ' + error); }
}

The problem is I always get POST erro - 400 - : Invalid JSON. I tried the same json with Chrome Rest Client and It works good. I don't know why I get this error. Is there a mistake on the array or the options?

signalpoint commented 6 years ago

@jsbriantes Try replacing this:

data: myformdata,

With this:

data: JSON.stringify(myformdata),
jsbriantes commented 6 years ago

Thanks very much. It works good.