【问题标题】:Post Raw Json via .submit()通过 .submit() 发布原始 Json
【发布时间】:2015-06-30 04:42:13
【问题描述】:

如何通过 form.submit() 将用户名/密码作为原始 JSON 发布。

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

即使Ext.JSON.encode(loginForm.getValues()) 服务器接收到的用户名=test&password=test 我需要是 {"username":"test","password":"test"}

【问题讨论】:

    标签: json extjs sencha-touch sencha-touch-2


    【解决方案1】:

    你应该尝试类似的东西

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

    Source for reference

    【讨论】:

    • 是的,这可能有效,但由于 API 设计不好,我无权访问它们,我必须完全按照 {"username":"test","password":"test 发布“}。添加数据:{"username":"test","password":"test"} 不起作用。
    • 我明白了,你看过另一个post吗?
    • 是的,我搜索了 google 可以找到的所有内容,它发布的内容类似于 url-form-encoded 参数,username=test&password=test 导致服务器上出现错误 500。我能够使用 php 和 chrome rest 客户端发布 JSON。
    • 我没有看到 Ext.Ajax.request({ 在你的回答中 :)
    【解决方案2】:

    提交表单时,并非以 JSON 格式提交。 为了提交 JSON 字符串,必须使用 Ext.Ajax.request。 http://www.sencha.com/forum/showthread.php?132082-jsonData-in-submit-action-of-form

    我只需要改变

    loginForm.submit({})
    

    Ext.Ajax.request({})
    

    并使用

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

    错误的文档。

    【讨论】:

    • 我认为这些文档非常广泛。适用于任何框架的最广泛的在线文档之一。是的,没有提及某些实现和设计替代方案,但这是我们可以应用的。
    【解决方案3】:

    与 ExtJS 的情况一样,有一个简单的答案。在 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
    });
    

    设置属性jsonSubmit: true

    那么当您使用loginForm.submit() 时,您的对象将作为 JSON 而不是表单参数提交。

    这是表单文档的链接: http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.form.Panel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多