我有以下JavaScript代码:
App.Views.AddPerson = Backbone.View.extend({
el: '#addPerson',
events: {
'submit': 'submit',
},
submit: function (e) {
e.preventDefault();
var newPersonName = $(e.currentTarget).find('input[type=text]').val();
var person = new App.Models.Person({ name: newPersonName, age: newAge, occupation: newOccupation });
this.collection.add(person);
this.$('#todo-title').val('');
$.ajax({
type: 'POST',
url: '@Url.Action("InsertRecord", "Home")',
data: newPersonName ,
success: function(data) {
alert(data);
},
error: function(){
alert("error");
}
});
}
});
在控制器中我有以下方法:
[HttpPost]
public ActionResult InsertRecord(string str)
{
return View();
}
但它没有执行ajax调用并引发错误。
答案 0 :(得分:0)
使用此
$.ajax({
type: 'POST',
url: '@Url.Action("InsertRecord", "Home")',
data: {str:newPersonName} , // use the same paramtre name as in Controller
success: function(data) {
alert(data);
},
error: function(){
alert("error");
}
});
希望有所帮助
答案 1 :(得分:0)
$.ajax({
type: 'POST',
url: window.location + "Home/InsertRecord",
data: {ID : newPersonName,
Name:newAge,
DeptID:newOccupation },
success: function(data) {
alert(data);
},
error: function(){
alert("error");
}
});
答案 2 :(得分:0)
试试这个:
submit: function (e) {
e.preventDefault();
var newPersonName = $(e.currentTarget).find('input[type=text]').val();
var person = new App.Models.Person({ name: newPersonName, age: newAge, occupation: newOccupation });
this.collection.add(person);
this.$('#todo-title').val('');
$.ajax({
type: 'POST',
url: window.location + "Home/InsertRecord",
data: {ID : newPersonName},
success: function(data) {
// Code after success },
error: function(){
alert("error");
}
});