sencha-touchsencha-touch-2sencha-touch-2.3

submitting a form to the server as json


I am trying to submit a form to the server with the params in JSON.

form.submit({
  url:'JSONSaveEntry',
  method:'POST'
});

but it sends everything as form-www-urlencoded.

I already checked that no field has isFile set to true (but then, it would send as multipart-formdata) and that standardSubmit is false.

I also tried to use

Ext.Ajax.request({
  url:'JSONSaveEntry',
  method:'POST',
  params:form.getValues()
});

and

Ext.Ajax.request({
  url:'JSONSaveEntry',
  method:'POST',
  params:Ext.encode(form.getValues())
});

Every submission is done as form-www-urlencoded, although the docs clearly state "Performs a Ajax-based submission of form values (if standardSubmit is false)". But then, this sentence is already proven wrong because whenever a file field is in the form, the form is submitted as multipart.

So, does anyone know how I can get the form submitted as JSON?

Possibility 2: I know that it works if I submit a model via model.save(), but how would I create a model from a form on-the-fly (without hardcoding the fields twice)?


Solution

  • I think below would solve your purpose.

    Ext.Ajax.request({
      url:'JSONSaveEntry',
      method:'POST',
      headers: { 'Content-Type': 'application/json' },
      jsonData : JSON.stringify(form.getValues()),
      success : function(response){ console.log("response from server")},
      failure : function(error){console.log(error)}
    });