application / json不起作用,但是text / json没有

时间:2017-03-07 06:55:27

标签: jquery json playframework

在下面的代码中,将ContentType设置为application / json不起作用(服务器发送Bad请求),但将其设置为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) {}
        })
    });
});

1 个答案:

答案 0 :(得分:0)

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