使用Knockout JS / JQuery将表单数据发布为Json

时间:2013-02-02 18:03:13

标签: json jquery knockout.js

当我尝试将表单数据以Json格式发布到服务器时,我遇到了一些错误。我的代码如下。

permissionRequestModel.requestPermission = function () {
if ($("#permissionRequestForm").valid()) {
  $.ajax({
          url: "http://ec2-107-20-7-114.compute-1.amazonaws.com/adco/api/Request/permission?    appid=1&opertype=requestor&employeeNo=1000416",
          type: "POST",
          data: ko.toJSON(this),
          processData:false,
          contentType: "application/json",
          dataType:"json",
          success: function (result) {
                    alert("Success");
                     },
         error: function (result) {
             alert(result.responseText);
                 }
           });
          }
    else {
        }
        }; 

淘汰模型如下

//Model
var permissionRequestModel = {
coNumber: ko.observable('1000416'),
employName: ko.observable('William Louis Langford (CITD)'),
fromDate: ko.observable(''),
toDate: ko.observable(''),
checkFullDay: ko.observable(false),
fromTimeHH: ko.observable(''),
fromTimeMM: ko.observable(''),
toTimeHH: ko.observable(''),
toTimeMM: ko.observable(''),
permissionTypeOne: ko.observable(''),
permissionTypeTwo: ko.observable(''),
approverList: ko.observableArray([]),
reasonLeave: ko.observable('')
}; 

在某些地方读取它可能是因为Json可能在发送到服务器之前被编码,但即使设置processData也无法解决它。请指导。

错误消息

{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an     instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":"   at  ADCO.API.Models.Tms.PermissionRequest.Apply() in      c:\\Users\\Predhin\\Dropbox\\ADCO\\ADCO.API\\ADCO.API\\Models\\Tms\\PermissionRequest.cs:li ne 62\r\n   at ADCO.API.Models.Common.WorkFlowManager.Process() in c:\\Users\\Predhin\\Dropbox\\ADCO\\ADCO.API\\ADCO.API\\Models\\Common\\WorkFlowManager.cs:line 29\r\n   at ADCO.API.Controllers.RequestController.Post(PermissionRequestModel reqPostData, Int32 appid, String requesttype, String opertype, String employeeNo) in c:\\Users\\Predhin\\Dropbox\\ADCO\\ADCO.API\\ADCO.API\\Controllers\\RequestController.cs:line 60\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}"

1 个答案:

答案 0 :(得分:2)

this更改为permissionRequestModel ...您的this不是模型,因为您不再处于模型的上下文中。你在函数内,this将返回函数。

permissionRequestModel.requestPermission = function () {
if ($("#permissionRequestForm").valid()) {
  $.ajax({
          url: "http://ec2-107-20-7-114.compute-1.amazonaws.com/adco/api/Request/permission?    appid=1&opertype=requestor&employeeNo=1000416",
          type: "POST",
          data: ko.toJSON(permissionRequestModel),
          processData:false,
          contentType: "application/json",
          dataType:"json",
          success: function (result) {
                    alert("Success");
                     },
         error: function (result) {
             alert(result.responseText);
                 }
           });
          }
    else {
        }
        };