【问题标题】:Alpacajs forms: how to set the content-type to json?Alpacajs 表单:如何将内容类型设置为 json?
【发布时间】:2017-11-20 12:32:25
【问题描述】:

提交 alpacajs 表单时,似乎没有指定内容类型标头的选项。

所有表单都以 application/x-www-form-urlencoded 的形式提交,这对我来说有点奇怪,因为 Alpaca 都是关于 json 的。它也没有在文档中列出。

所以我的问题是:如何将表单提交为 JSON 内容类型?

Laravel 在其文档“When sending JSON requests to your application, you may access the JSON data via the input method as long as the Content-Type header of the request is properly set to application/json”中声明了以下内容。

【问题讨论】:

    标签: javascript alpacajs


    【解决方案1】:

    类似

    var data = $('#my-form').alpaca().getValue();
    
    $.ajax('/endpoint', {
      contentType: 'application/json',
      data: JSON.stringify(data)
    })
    

    或者

    $('#my-form').alpaca({
      schema: {..},
      options: {
        form: {
          buttons: {
            submit: {
              type: 'button',
              click: function(){
                var val = this.getValue();
                // Repeat ajax call from above.
              }
            }
          }
        }
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 1970-01-01
      • 2021-01-04
      • 2019-03-22
      • 1970-01-01
      • 2012-02-28
      • 2023-03-06
      • 2019-03-01
      • 2017-05-06
      相关资源
      最近更新 更多