【问题标题】:submitting a form to the server as json将表单作为 json 提交到服务器
【发布时间】:2015-01-15 15:02:27
【问题描述】:

我正在尝试使用 JSON 格式的参数向服务器提交表单。

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

但它会以 form-www-urlencoded 的形式发送所有内容。

我已经检查过没有将isFile 设置为true 的字段(但是,它会以multipart-formdata 发送)并且standardSubmitfalse

我也试过

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

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

每次提交都以form-www-urlencoded 完成,尽管文档明确指出“执行基于 Ajax 的表单值提交(如果 standardSubmit 为 false)”。但是,这句话已经被证明是错误的,因为只要文件字段在表单中,表单就会作为多部分提交。

那么,有谁知道我如何将表单提交为 JSON?

可能性 2:我知道如果我通过 model.save() 提交模型,它会起作用,但是我如何从动态表单创建模型(无需对字段进行两次硬编码)?

【问题讨论】:

  • 如果你给了一个参数对象,比如params:{myJson:Ext.encode(form.getValues())},然后在服务器端 JSON_decode 参数应该给你你的 JSON 对象呢?

标签: sencha-touch sencha-touch-2 sencha-touch-2.3


【解决方案1】:

我认为下面会解决你的目的。

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)}
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-12
    • 2011-12-19
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多