JQuery的AJAX'后'的数据是不是使之通过向网络API控制器使之、控制器、数据、网络

2023-09-10 22:17:54 作者:至死不休

我一个人()对象设置一对夫妇ko.observables,并试图将其传递给我的ajax调用。在另一边,一个Web API控制器及其过来为空。

这是我怎么敢数据绑定到ko.observables:

 <标签>名字< /标签>
<输入类型=文本级=形式控制利润率底-20的数据绑定=文字:名字>

<标签>姓< /标签>
<输入类型=文本级=形式控制利润率底-20的数据绑定=文字:名字>

<标签>电子邮件地址<跨度类=彩红> *< / SPAN>< /标签>
<输入类型=文本级=形式控制利润率底-20的数据绑定=文本:通过电子邮件将>
 

我的.js是想拿起ko.observable值,但不是:

  VAR姓= ko.observable();
VAR姓氏= ko.observable();
VAR电子邮件= ko.observable();
VAR密码= ko.observable();


功能submitclicked(){
    insertNewUser();

    ko.applyBindings(名字);
    ko.applyBindings(姓氏);
    ko.applyBindings(电子邮件);
    ko.applyBindings(密码);
};

功能的人(){
    this.FirstName =名字();
    this.LastName =姓氏();
    this.Email =电子邮件();
    this.Password = PASSWORD();
}

功能insertNewUser(){

变种人=新的Person();

$阿贾克斯({
    网址:/ API /注册,
    类型:'后',
    数据:JSON.stringify(人),
    的contentType:应用/ JSON的;字符集= UTF-8,
    成功:函数(结果){
    人(结果);
    }
})
 
jQuery Ajax

我的控制器,它是某种不拉在那个人的对象正常。它得到的对象,但所有的值都为空(即使文本输入确实有文字) =将First_Name空,姓氏= NULL,电子邮件= NULL,密码= NULL

 公共无效ADDUSER(个人项目)
{
    变种DB =新MyEntities();

    尝试
    {
    用户记录=新用户()
    {
        =将First_Name item.FirstName,
        姓氏= item.LastName,
        电子邮件= item.Email,
        密码= item.Password
    };
    db.User.Add(记录);
    db.SaveChanges();
 

任何通知,我已经搞砸了?

编辑:清理这个例子有点

解决方案

 公共.....帖子([FromBody] objClass OBJ)
  VAR的事情= obj.property;
 

你也不能发布一个对象(它需要真实的数据),看到的Json字符串化试试这个线程How以JSON.stringify和JSON.parse没有得到一个空对象?

I'm setting a Person() object to a couple of ko.observables and trying to pass it into my ajax call. On the other side, a web API controller its coming over as null.

This is how I'm data-binding to the 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:

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

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?