简体   繁体   English

jQuery AJAX“发布”数据无法传递到Web api控制器

[英]JQuery AJAX 'post' data isn't making it through to the web api controller

I'm setting a Person() object to a couple of ko.observables and trying to pass it into my ajax call. 我正在将一个Person()对象设置为几个ko.observables,并试图将其传递给我的ajax调用。 On the other side, a web API controller its coming over as null. 另一方面,一个Web API控制器以null的形式出现。

This is how I'm data-binding to the ko.observables: 这就是我将数据绑定到ko.observables的方式:

<label>First Name</label>
<input type="text" class="form-control margin-bottom-20" data-bind="text: firstname">

<label>Last Name</label>
<input type="text" class="form-control margin-bottom-20" data-bind="text: lastname">

<label>Email Address <span class="color-red">*</span></label>
<input type="text" class="form-control margin-bottom-20" data-bind="text: email">

my .js that is suppose to pick up the ko.observable values but isn't: 我的.js应该是选择ko.observable的值,但不是:

var firstname = ko.observable();
var lastname = ko.observable();
var email = ko.observable();
var password = ko.observable();


function submitclicked() {
    insertNewUser();

    ko.applyBindings(firstname);
    ko.applyBindings(lastname);
    ko.applyBindings(email);
    ko.applyBindings(password);
};

function Person() {
    this.FirstName = firstname();
    this.LastName = lastname();
    this.Email = email();
    this.Password = password();
}

function insertNewUser() {

var person = new Person();

$.ajax({
    url: "/api/Reg",
    type: 'post',
    data: JSON.stringify(person),
    contentType: "application/json; charset=utf-8",
    success: function (result) {
    person(result);
    }
})

My controller which is somehow not pulling in that "person" object properly. 我的控制器以某种方式无法正确引入该“人”对象。 it gets the objects but all of the values are null (even though the text inputs do have text) First_Name = null, Last_Name = null, Email = null, Password = null 它获取对象,但所有值均为空(即使文本输入中确实包含文本)First_Name = null,Last_Name = null,Email = null,Password = null

public void addUser(Person item)
{
    var db = new MyEntities();

    try
    {
    User record = new User()
    {
        First_Name = item.FirstName,
        Last_Name = item.LastName,
        Email = item.Email,
        Password = item.Password
    };
    db.User.Add(record);
    db.SaveChanges();

Anyone notice where I've messed it up? 有人注意到我搞砸了吗?

EDIT: cleaned up the example a bit. 编辑:清理了一下示例。

  public ..... Post([FromBody]objClass obj)
  var thing=obj.property;

Also you cant post an object (it needs to be real data) , see Json stringify try this thread How to JSON.stringify and JSON.parse without getting an empty object? 另外,您也不能发布对象(它必须是真实数据),请参阅Json stringify尝试此线程如何在不获取空对象的情况下进行JSON.stringify和JSON.parse?

Use ko.toJSON it will correctly serialize the observables to plain JSON data 使用ko.toJSON它将正确将可观察对象序列化为纯JSON数据

Also you model definition is strange. 另外您的模型定义很奇怪。 To define the observables outside of the constructor, this mean that you cant have more than one instance of the Person object 要在构造函数之外定义可观察对象,这意味着您不能拥有多个Person对象的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM