Javascript 无法从WEB API项目中使用的Knockout.js视图模型发送JSON调用
从我的Knockout.js模型函数发送AJAX调用时,我遇到了一些问题 我觉得这很奇怪。我的淘汰脚本看起来像Javascript 无法从WEB API项目中使用的Knockout.js视图模型发送JSON调用,javascript,knockout.js,asp.net-web-api,knockout-mvc,Javascript,Knockout.js,Asp.net Web Api,Knockout Mvc,从我的Knockout.js模型函数发送AJAX调用时,我遇到了一些问题 我觉得这很奇怪。我的淘汰脚本看起来像 <script type="text/javascript"> var userinfoViewModel = function () { var self = this; self.ID = ko.observable("0"); self.First_Name = ko.observable("");
<script type="text/javascript">
var userinfoViewModel = function () {
var self = this;
self.ID = ko.observable("0");
self.First_Name = ko.observable("");
self.Last_Name = ko.observable("");
self.Login_Id = Ko.observable("");
self.Password = ko.observable("");
self.Role = ko.observable("");
var user = {
ID: self.ID,
First_Name: self.First_Name,
Last_Name: self.Last_Name,
Login_Id: self.Login_Id,
Password: self.Password,
Role: self.Role
};
//Declare an ObservableArray for Storing the JSON Response
self.Users = ko.observableArray([]);
//Function to Read All Users
function GetUsers() {
//Ajax Call Get All Employee Records
$.ajax({
type: "GET",
url: "/api/user",
contentType: "application/json",
dataType: "json",
success: function (data) {
self.Users(data); //Put the response in ObservableArray
},
error: function (error) {
alert(error.status + "<--and--> " + error.statusText);
}
});
//Ends Here
}
//call GetUsers Ajax call.
GetUsers();
//CRUD Operations
//Function to perform POST (insert User) operation
self.Create = function () {
//Ajax call to Insert the User
$.ajax({
type: "POST",
url: "/api/user",
data: ko.toJSON(User), //Convert the Observable Data into JSON
contentType: "application/json",
success: function (data) {
alert("Record Added Successfully");
self.EmpNo(data.EmpNo);
alert("The New Employee Id :" + self.EmpNo());
GetEmployees();
},
error: function () {
alert("Failed");
}
});
//Ends Here
};
self.update = function () {
var url = "/api/user/" + self.EmpNo();
alert(url);
$.ajax({
type: "PUT",
url: url,
data: ko.toJSON(EmpData),
contentType: "application/json",
success: function (data) {
alert("Record Updated Successfully");
GetEmployees();
},
error: function (error) {
alert(error.status + "<!----!>" + error.statusText);
}
});
};
//Function to perform DELETE Operation
self.deleterec = function (user) {
$.ajax({
type: "DELETE",
url: "/api/EmployeeInfoAPI/" + employee.EmpNo,
success: function (data) {
alert("Record Deleted Successfully");
GetUser();//Refresh the Table
},
error: function (error) {
alert(error.status + "<--and--> " + error.statusText);
}
});
// alert("Clicked" + employee.EmpNo)
};
//Function to Display record to be updated
self.getselecteduser = function (user) {
self.ID(user.ID),
self.First_Name(user.First_Name),
self.Last_Name(user.Last_Name),
self.Login_Id(user.Login_Id),
self.Password(user.Password),
self.Role(user.Role)
};
};
ko.applyBindings(new userinfoViewModel());
</script>
}
var userinfoViewModel=函数(){
var self=这个;
self.ID=ko.可观察(“0”);
self.First_Name=ko.observable(“”);
self.Last_Name=ko.可观察(“”);
self.Login_Id=Ko.observable(“”);
self.Password=ko.observable(“”);
self.Role=ko.observable(“”);
变量用户={
ID:self.ID,
名字:self.First\u Name,
姓氏:self.Last_Name,
Login\u Id:self.Login\u Id,
密码:self.Password,
角色:自我。角色
};
//声明用于存储JSON响应的ObservableArray
self.Users=ko.observearray([]);
//函数读取所有用户
函数GetUsers(){
//Ajax调用获取所有员工记录
$.ajax({
键入:“获取”,
url:“/api/user”,
contentType:“应用程序/json”,
数据类型:“json”,
成功:功能(数据){
self.Users(data);//将响应放在ObservableArray中
},
错误:函数(错误){
警报(error.status+“”+error.statusText);
}
});
//到此为止
}
//调用GetUsers Ajax调用。
GetUsers();
//积垢作业
//执行POST(插入用户)操作的函数
self.Create=函数(){
//Ajax调用以插入用户
$.ajax({
类型:“POST”,
url:“/api/user”,
data:ko.toJSON(用户),//将可观察数据转换为JSON
contentType:“应用程序/json”,
成功:功能(数据){
警报(“成功添加记录”);
self.EmpNo(data.EmpNo);
警报(“新员工Id:+self.EmpNo());
GetEmployees();
},
错误:函数(){
警报(“失败”);
}
});
//到此为止
};
self.update=函数(){
var url=“/api/user/”+self.EmpNo();
警报(url);
$.ajax({
键入:“放置”,
url:url,
数据:ko.toJSON(EmpData),
contentType:“应用程序/json”,
成功:功能(数据){
警报(“记录更新成功”);
GetEmployees();
},
错误:函数(错误){
警报(error.status+“”+error.statusText);
}
});
//警报(“单击”+employee.EmpNo)
};
//用于显示要更新的记录的函数
self.getselecteduser=函数(用户){
self.ID(user.ID),
自身名(用户名),
self.Last_Name(user.Last_Name),
self.Login\u Id(user.Login\u Id),
self.Password(用户密码),
self.Role(user.Role)
};
};
applyBindings(新的userinfoViewModel());
}
我在页面中使用的底层HTML如下所示-
<form>
<table>
<tr>
<td>
<!--Bind the TextBoxes in the Table to the observable properties defined into the ViewModel -->
<table id="tbldml">
<tr>
<td>ID</td>
<td>
<input type="text" id="ID" data-bind="value: $root.ID" /></td>
</tr>
<tr>
<td>First_Name</td>
<td>
<input type="text" id="First_Name" data-bind="value: $root.First_Name" /></td>
</tr>
<tr>
<td>Last_Name</td>
<td>
<input type="text" id="Last_Name" data-bind="value: $root.Last_Name" /></td>
</tr>
<tr>
<td>Login_Id</td>
<td>
<input type="text" id="Login_Id" data-bind="value: $root.Login_Id" /></td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="text" id="Password" data-bind="value: $root.Password" />
</td>
</tr>
<tr>
<td>Role</td>
<td>
<input type="text" id="Role" data-bind="value: $root.Role" />
</td>
</tr>
<tr>
<!--The click binding has the JavaScirpt methods passed to it-->
<td>
<button data-bind="click :$root.save">Save</button></td>
<td>
<button data-bind="click: $root.update">Update</button></td>
</tr>
</table>
</td>
<td>
<div class="FixedContainer">
<!--If the lenght of the Users is greater than 0 then visible the Table-->
<table data-bind="visible: Users().length>0">
<thead>
<tr>
<td>ID</td>
<td>First_Name</td>
<td>Last_Name</td>
<td>Login_Id</td>
<td>Role</td>
<td></td>
</tr>
</thead>
<!--Iterate through an observableArray using foreach-->
<tbody data-bind="foreach: Users">
<tr data-bind="click: $root.getselecteduser" id="updtr">
<td><span data-bind="text: ID"></span></td>
<td><span data-bind="text: First_Name"></span></td>
<td><span data-bind="text: Last_Name"></span></td>
<td><span data-bind="text: Login_Id"></span></td>
<td><span data-bind="text: Role"></span></td>
<td>
<button data-bind="click: $root.deleterec">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
</form>
</section>
身份证件
名字
姓
登录标识
密码
角色
拯救
更新
身份证件
名字
姓
登录标识
角色
删除
这个问题看起来很奇怪,不过我想,绑定是非常合适的。
我的API调用URL类似于-Http://localhost:1234/api/user
Fiddler track:我试着通过
self.Login_Id = Ko.observable(""); // must be 'ko'
function User(data){
self = this;
self.ID = ko.observable(data.ID);
self.First_Name = ko.observable(data.First_Name);
self.Last_Name = ko.observable(data.Last_Name);
self.Login_Id = Ko.observable(data.Login_Id);
self.Password = ko.observable(data.Password);
self.Role = ko.observable(data.Role);
}
var userinfoViewModel = function () {
var self = this;
//Declare an ObservableArray for Storing the JSON Response
self.Users = ko.observableArray([]);
...
in your CREATE function loop through the AJAX response:
for(user in ajaxData){
var newUser = new User(user);
}
//then push it onto the Users array
self.Users.push(newUser);
...
other CRUD functions
...
}