Javascript xmlHttpRequest AJAX POST读取MVC控制器中的发布值
尽管有大量有用的信息,但我们一直在努力寻找解决方案 我有下面的Javascript函数,它向我的控制器发送一个AJAX调用Javascript xmlHttpRequest AJAX POST读取MVC控制器中的发布值,javascript,ajax,asp.net-mvc,Javascript,Ajax,Asp.net Mvc,尽管有大量有用的信息,但我们一直在努力寻找解决方案 我有下面的Javascript函数,它向我的控制器发送一个AJAX调用 var ajax = new XMLHttpRequest(); var posts = document.getElementsByClassName('postLink'); var sendstr = ""; for (var i = 0; i < posts.length; i++) {
var ajax = new XMLHttpRequest();
var posts = document.getElementsByClassName('postLink');
var sendstr = "";
for (var i = 0; i < posts.length; i++) {
sendstr += ',';
sendstr += posts[i].attributes.getNamedItem('onmouseup').value.substr(9, 1);
}
ajax.onreadystatechange = function () {
if (ajax.readyState == 4 && ajax.status == 200) {
var posts = JSON.parse(ajax.responseText);
addPostToList(posts.Title, posts.Description, posts.postID);
}
}
ajax.open('POST', '@Url.Action("SAR", "posts")', true);
ajax.setRequestHeader('Content-type', 'text/plain');
ajax.setRequestHeader('Content-length', sendstr.length);
ajax.setRequestHeader('Connection', 'close');
ajax.send(encodeURIComponent(sendstr));
我尝试了许多不同的数据类型作为参数,但仍然没有收到任何有用的。。。我还尝试了Request.Param、Request.queryString、Request.Form和我在web上找到的许多其他“解决方案”,它只将逗号分隔的整数列表传递给控制器,存储为字符串。当该值已发送到控制器时,如何读取该值
~我不想要JQuery或JS框架解决方案,我想要本机Javascript解决方案。1。-使用JSON作为内容类型 2.-返回的字符串必须与控制器上预期的参数名称匹配
var ajax = new XMLHttpRequest();
var posts = document.getElementsByClassName('postLink');
var sendstr = "";
for (var i = 0; i < posts.length; i++) {
sendstr += ',';
sendstr += posts[i].attributes.getNamedItem('onmouseup').value.substr(9, 1);
}
ajax.onreadystatechange = function () {
if (ajax.readyState == 4 && ajax.status == 200) {
var posts = JSON.parse(ajax.responseText);
addPostToList(posts.Title, posts.Description, posts.postID);
}
}
ajax.open('POST', '@Url.Action("SAR", "posts")', true);
ajax.setRequestHeader('Content-type', 'text/plain');
ajax.setRequestHeader('Content-length', sendstr.length);
ajax.setRequestHeader('Connection', 'close');
ajax.send(encodeURIComponent(sendstr));
将sendstr更改为以下格式:
//Where r is the name of your parameter on the controller.
var sendstr = '{"r":"STRING YOU WANT TO RETURN"}';
然后将ajax配置更改为
ajax.open('POST', '@Url.Action("SAR","Controller")', true);
ajax.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
ajax.setRequestHeader('Content-Length', sendstr.length);
ajax.send(sendstr);
我希望这对您有用:)1。-使用JSON作为您的内容类型 2.-返回的字符串必须与控制器上预期的参数名称匹配
var ajax = new XMLHttpRequest();
var posts = document.getElementsByClassName('postLink');
var sendstr = "";
for (var i = 0; i < posts.length; i++) {
sendstr += ',';
sendstr += posts[i].attributes.getNamedItem('onmouseup').value.substr(9, 1);
}
ajax.onreadystatechange = function () {
if (ajax.readyState == 4 && ajax.status == 200) {
var posts = JSON.parse(ajax.responseText);
addPostToList(posts.Title, posts.Description, posts.postID);
}
}
ajax.open('POST', '@Url.Action("SAR", "posts")', true);
ajax.setRequestHeader('Content-type', 'text/plain');
ajax.setRequestHeader('Content-length', sendstr.length);
ajax.setRequestHeader('Connection', 'close');
ajax.send(encodeURIComponent(sendstr));
将sendstr更改为以下格式:
//Where r is the name of your parameter on the controller.
var sendstr = '{"r":"STRING YOU WANT TO RETURN"}';
然后将ajax配置更改为
ajax.open('POST', '@Url.Action("SAR","Controller")', true);
ajax.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
ajax.setRequestHeader('Content-Length', sendstr.length);
ajax.send(sendstr);
我希望这对您有用:)有很多解决方案。主要问题是您没有告诉控制器它应该将您发送的数据绑定到变量
r
ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded');send(“r=“+encodeURIComponent(sendstr))代码>应该是最简单的修复你的代码作品像一个魅力,我感谢你!有很多解决办法。主要问题是您没有告诉控制器它应该将您发送的数据绑定到变量r
ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded');send(“r=“+encodeURIComponent(sendstr))代码>应该是最简单的修复你的代码作品像一个魅力,我感谢你!