jquery - 从主控制器上的 Jquery/Ajax 检索地图功能数据
问题描述
在我的 .cshtml 上,我在容器内获取了用户以这种方式选择的所有按钮:
<script>
$("#createnewidx").click(function (e) {
console.log('here111');
var selected = $("#WholeTagBlock input:checked").map(function (i, el) { return el.name; }).get();
alert("selected = [" + selected + "]\nas string = \"" + selected.join(";") + "\"");
$.ajax({
url: '@Url.Action("CreateNewIdx")',
data: selected.serializeArray(),
type: 'POST',
});
console.log('here333');
});
</script>
在我的“选择”上,我能够获得用户选择的所有按钮。现在,我需要将此信息传递给我的控制器。有了这个,我发现最直接的想法可能是使用 Jquery Post(例如,在这里尝试这样的想法)。
我的主要目标是将此数据发送到“公共 ActionResult CreateNewIdx”,这是我将用于将此数据插入数据库的函数。
我也尝试使用这个想法:
$.ajax({
type: 'POST',
url: '@Url.Action("CreateNewIdx")',
data: { 'email': 'che607@yahoo.com' },
dataType: 'json',
cache: false,
contentType: "application/jsonrequest; charset=utf-8",
success: function (data) {
console.log('SUCCESS: ', data);
},
error: function (data) {
console.log('ERROR: ', data);
},
});
我的问题是,当我尝试在我的操作上检索数据时,它总是为空。我在检索时的想法是:
public ActionResult CreateNewIdx(String email) (来自上面的例子) public ActionResult CreateNewIdx(String FixedTag) (或“email2”,下一个例子):
$.ajax({
url: '@Url.Action("CreateNewIndex")',
type: "POST",
dataType: 'json',
data: { 'email2': selected},
cache: false,
contentType: "application/jsonrequest; charset=utf-8",
success: function (data) {
alert("hi" + data);
console.log('SUCCESS2: ', data);
}
});
按照一条评论的想法,我尝试了:
$.ajax({
type: 'POST',
url: '@Url.Action("CreateNewIndex")', // the method we are calling
data: JSON.stringify({ 'person' : 'che607@yahoo.com' }),
success: function (data) {
//alert('Yay! It worked!');
console.log('SUCCESS: ', data);
},
error: function (data) {
//alert('Oh no :(');
console.log('ERROR: ', data);
}
});
最后,我尝试了:
$.ajax({
type: 'POST',
url: '@Url.Action("CreateNewIndex")', // the method we are calling
data: JSON.stringify({ person: selected }),
success: function (data) {
//alert('Yay! It worked!');
console.log('SUCCESS: ', data);
},
error: function (data) {
//alert('Oh no :(');
console.log('ERROR: ', data);
}
});
添加内容类型:“应用程序/json”:
$.ajax({
type: 'POST',
url: '@Url.Action("CreateNewIndex")', // the method we are calling
contentType: "application/json",
data: JSON.stringify({ 'person4': 'testing11'}),
success: function (data) {
//alert('Yay! It worked!');
console.log('SUCCESS: ', data);
},
error: function (data) {
//alert('Oh no :(');
console.log('ERROR: ', data);
}
});
并且:
$.ajax({
type: 'POST',
url: '@Url.Action("CreateNewIndex")', // the method we are calling
contentType: "application/json",
data: JSON.stringify({ person3: selected}),
success: function (data) {
//alert('Yay! It worked!');
console.log('SUCCESS: ', data);
},
error: function (data) {
//alert('Oh no :(');
console.log('ERROR: ', data);
}
});
我错过了什么吗?
解决方案
推荐阅读
- java - What should be the name of the prev_node argument passed into the method given below?
- mysql - 更改端口号后phpMyAdmin登录不起作用
- batch-file - 为什么我的批处理文件在 Windows 10 上停止工作?
- pandas - 如何在熊猫数据框中提取组的最后一个值
- python - 使用 aiohttp 的多个请求,但每个请求单独超时
- ruby-on-rails - 我如何使用 rspec 运行测试,使用 simple_token_authentication 并设计
- http - 为什么以下代码中的最后一个错误处理程序存在无效参数错误?
- c - 除了字母值之外,错误字符串变量正在接收 0x603270
- javascript - 如何在 React Hook 中添加自定义类名?
- regex - 为什么有grep的时候find命令有-regex?