2016-01-12 49 views
2

我在我的应用程序中使用角度。最后得到了现在试图执行该帖子的处理。我的问题是使用$ http.post。

我的错误是ExceptionType:“System.ArgumentException” 消息:“无效的JSON基元:json。”当我不包含配置对象。

当我做包括配置对象,则错误是“的WebMethod名称是无效的”

这里是代码。

 vm.createCustomer = function() { 
     var customerObject = {}; 
     customerObject.firstName = $("#firstName").val(); 
     customerObject.lastName = $("#lLastName").val(); 
     customerObject.company = $("#company").val(); 

     var data = $.param({ 
      json: JSON.stringify(customerObject) 
     }); 

     $http.post("DAL/WebService.asmx/InsertCustomer", data).success(function(data, status){ 
      toastr.options = { 
       "positionClass": "toast-top-right", 
      } 
      toastr.success("Created"); 
     }) 
    } 

的WebMethod

 [WebMethod] 
    public void InsertCustomer(Customer c) 
    { 
     string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(cs)) 
     { 
      SqlCommand cmd = new SqlCommand("InsertCustomer", con); 
      cmd.CommandType = CommandType.StoredProcedure; 

      cmd.Parameters.Add(new SqlParameter() 
      { 
       ParameterName = "firstName", 
       Value = c.firstName 
      }); 
      cmd.Parameters.Add(new SqlParameter() 
      { 
       ParameterName = "lastName", 
       Value = c.lastName 
      }); 
      cmd.Parameters.Add(new SqlParameter() 
      { 
       ParameterName = "company", 
       Value = c.company 
      }); 

      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
    } 
} 
+0

为什么不'$ http.post( “DAL/WebService.asmx/InsertCustomer”,customerObject )'? –

+0

然后创建缺少参数 –

+0

'$ http.post(“DAL/WebService.asmx/InsertCustomer”,$ .param(customerObject))''? –

回答

0

唉只花了2.5小时找出语法

  $http.post("DAL/WebService.asmx/InsertCustomer", {c: customerObject}).then(function (customerObject, status) { 
      toastr.options = { 
       "positionClass": "toast-top-right", 
      } 
      toastr.success("Created"); 
     })