[英]Posting form data to a controller method using Ajax
THE PROBLEM 问题
I want to post form data to a controller asynchronously using AJAX. 我想使用AJAX异步地将表单数据发布到控制器。 Here's a simplified view of my JavaScript:
这是我的JavaScript的简化视图:
function SendForm() {
var formData = new FormData();
formData.append('value1', 'hello');
formData.append('value2', 'world');
var xhr = new XMLHttpRequest();
xhr.upload.onload = function() {
// stuff that happens on the callback
};
xhr.open('POST', 'http://server/controller/SomeAction', true);
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.send(formData);
}
The problem begins on the server side where all the method parameters are null. 问题始于服务器端,其中所有方法参数均为null。
[HttpPost]
public ContentResult SomeAction(string value1, string value2)
{
if(String.IsNullOrEmpty(value1) || String.IsNullOrEmpty(value2)) { throw new Exception("World not found."); }
return Content("something");
}
THE QUESTION 问题
My question is, why are all the method parameters null? 我的问题是,为什么所有方法参数都为null?
EXTRA RESEARCH 额外研究
I've looked inside the request stream, and I can see from the length that there is content there, but for whatever reason MVC has failed to match the parameters I specified in the FormData object to the parameters in the methed in the controller. 我查看了请求流,我可以从长度上看到那里有内容,但无论出于什么原因,MVC都无法将我在FormData对象中指定的参数与控制器中的methed中的参数相匹配。
WHY AM I DOING THIS? 我为什么这样做?
Yes, I want to POST the data. 是的,我想发布数据。 I've simplified the example, but I want to post more data than can be placed on a URL, so a REST method (passing the data on the querystring) is not acceptable for this solution.
我已经简化了示例,但是我希望发布的数据多于可以放在URL上的数据,因此对于此解决方案,REST方法(在查询字符串上传递数据)是不可接受的。
try to make the ajax call this way, i think it is the best way to make request 尝试以这种方式进行ajax调用,我认为这是提出请求的最佳方式
function SendForm() {
var data = {};
data.value1 = "Hello";
data.value2 = "World";
$.ajax({
url: "http://server/controller/SomeAction",
type: "POST",
dataType: "json",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8;",
success: function (data) {
// your code in success
alert("success");
}
});
}
and the action method will be 而动作方法将是
[HttpPost]
public JsonResult SomeAction(string value1, string value2)
{
if(String.IsNullOrEmpty(value1) || String.IsNullOrEmpty(value2))
{
throw new Exception("World not found.");
}
return Json("something");
}
function SendForm() {
var formData = new FormData();
formData.append('value1', 'hello');
formData.append('value2', 'world');
$.ajax({
url: "http://server/controller/SomeAction",
type: 'post',
cache: false,
contentType: false,
processData: false,
data: formData,
success: function (data) {
// ..... any success code you want
}
});
function SendForm() {
var formData = new FormData();
formData.append('value1', 'hello');
formData.append('value2', 'world');
$.ajax({
url: "http://server/controller/SomeAction",
type: 'post',
cache: false,
contentType: false,
processData: false,
data: { formData: formData, value1: "hello", value2: "world"},
success: function (data) {
// ..... any success code you want
}
});
And Recieve JSON at server side
并在服务器端接收JSON
Please try this .I am Unable to run the code on a project now.This is the theory though ,You can pass the data seperately using AJAX 请尝试这个。我现在无法在项目上运行代码。这是理论,但你可以使用AJAX单独传递数据
If Only value1 and value2 is reqd - Server
如果只需要value1和value2 - 服务器
[HttpPost]
public JsonResult SomeAction(string value1, string value2)
{
if(String.IsNullOrEmpty(value1) || String.IsNullOrEmpty(value2)) { throw
new Exception("World not found."); }
return Content("something");
}
JQuery
JQuery的
$.ajax({
url: "http://server/controller/SomeAction",
type: 'post',
cache: false,
contentType: false,
processData: false,
data: { value1: "hello", value2: "world"},
success: function (data) {
// ..... any success code you want
}
});
Try using AJAX this way, 尝试以这种方式使用AJAX,
function SendForm() {
$.ajax({
type: 'POST',
url: '/controller/SomeAction',
data: { value1 : 'hello', value2 : 'world' },
success: function (result) {
//Your success code here..
},
error: function (jqXHR) {
if (jqXHR.status === 200) {
alert("Value Not found");
}
}
});
}
You can find the value1
and value2
to server side. 您可以在服务器端找到
value1
和value2
。
Controller 调节器
[HttpPost]
public ContentResult SomeAction(string value1, string value2)
{
if(String.IsNullOrEmpty(value1) || String.IsNullOrEmpty(value2)) { throw new Exception("World not found."); }
return Content("something");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.