0

How can I post username/password as Raw JSON via form.submit().

loginForm.submit({  
    url: 'localhost/login',
    method: 'post',
    jsonData: loginForm.getValues()
...
    success: ...

even with Ext.JSON.encode(loginForm.getValues()) server is receiving as username=test&password=test which I need to be {"username":"test","password":"test"}

flakerimi
  • 2,580
  • 3
  • 29
  • 49

3 Answers3

3

You should probably try something like

Ext.Ajax.request({
    method: 'POST',
    url: 'submit.php',
    params  : {
        data: Ext.encode(loginForm.getValues())
    },
    success: function() {
    },
    failure: function() {
    }
});

Source for reference

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • Yeah this might work, but since there are bad api design, which I dont have access to them, I have to post exactly as {"username":"test","password":"test"}. adding data: {"username":"test","password":"test"} wont work. – flakerimi Apr 22 '15 at 14:01
  • 1
    i see, have you looked at this other [post](http://stackoverflow.com/questions/2917581/how-to-post-json-data-with-extjs)? – Scriptable Apr 22 '15 at 14:08
  • Yeah, I searched every thing that google can find, it is posting like url-form-encoded params, username=test&password=test which is causing Error 500 on server. I was able to post JSON with php and chrome rest client. – flakerimi Apr 22 '15 at 14:23
  • I didnt see Ext.Ajax.request({ on your answer :) – flakerimi Apr 22 '15 at 16:57
2

As is often the case with ExtJS, there's an easy answer. In the config of the loginForm:

Ext.create('Ext.form.Panel', {
   title: 'Simple Form',

  // The form will submit an AJAX request to this URL when submitted
  url: 'save-form.php',

  .... (blah blah blah)

  jsonSubmit: true    // <---- set this to TRUE
});

Set the property jsonSubmit: true.

Then when you use loginForm.submit(), your object will be submitted as JSON rather than form params.

Here's a link to the form docs: http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.form.Panel

Patrick Chu
  • 1,513
  • 14
  • 15
0

When a form is submitted, it is not submitted in JSON format. In order to submit JSON string, Ext.Ajax.request must be used. http://www.sencha.com/forum/showthread.php?132082-jsonData-in-submit-action-of-form

I just had to change

loginForm.submit({})

to

Ext.Ajax.request({})

and use

params: Ext.JSON.encode(loginForm.getValues()),

Bad DOCS.

flakerimi
  • 2,580
  • 3
  • 29
  • 49
  • I think the docs are pretty extensive. One of the most extensive online documents for any framework. Yes, certain implementation and design alternatives are not mentioned but that's for us to apply. – Yellen Apr 23 '15 at 06:32