【问题标题】:application/json doesn't work but text/json doesapplication/json 不起作用,但 text/json 起作用
【发布时间】:2017-03-07 06:55:26
【问题描述】:

在以下代码中,将 ContentType 设置为 application/json 不起作用(服务器发送错误请求),但将其设置为 text/json 可以。为什么?

/*converts form Array to JSON*/
function objectifyForm(formArray) {//serialize data function

  returnArray = {};
  for (var i = 0; i < formArray.length; i++){
    returnArray[formArray[i]['name']] = formArray[i]['value'];
  }
  return returnArray;
}

$(document).ready(function(){
    // click on form submit
    $('#registration-form').on('submit',function(e){
    e.preventDefault();
    var details = JSON.stringify(objectifyForm($(this).serializeArray()));
    console.log(details)
        // send ajax
        $.ajax({
            url: '/newreg', // url where to submit the request
            type : "POST", // type of action POST || GET
            datatype : "json", // data type
            /* this doesn't work*/
            //contentType: "application/json; charset=utf-8",
            /*this does*/
            contentType: "text/json; charset=utf-8",
            data : details,
/*handle success/failure*/
            success : function(result) {},
            error: function(xhr, resp, text) {}
        })
    });
});

【问题讨论】:

    标签: jquery json playframework


    【解决方案1】:

    Play Framework 后端可能存在错误或未配置的BodyParser

    【讨论】:

      猜你喜欢
      • 2017-10-10
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2013-04-03
      • 2014-05-17
      相关资源
      最近更新 更多