Alpacajs形式:如何将内容类型设置为json?

时间:2017-11-20 12:32:26

标签: javascript alpacajs

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

所有表单都以 application / x-www-form-urlencoded 提交,这有点奇怪我的口味,因为羊驼是关于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 ”中说明了以下内容。

1 个答案:

答案 0 :(得分: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.
          }
        }
      }
    }
  }
})