mvc接收ajax post一个参数值,从控制器中的ajax POST空接收参数+在firebug MVC 4中传递参数(Received parameter from ajax POST empty...

从控制器中的ajax POST空接收参数+在firebug MVC 4中传递参数(Received parameter from ajax POST empty in controller + passed parameter in firebug MVC 4)

我查看了网络,弄清楚我的错误是什么。 我发现我尝试的所有建议都没有任何成功。 我在我的控制器中访问httppost动作,但参数保持为空。

AJAX功能

var dataPost = { 'id': id, 'val': val };

debugger;

$.ajax({

type: 'POST',

url: '/Extensions/UpdateJson',

data: dataPost ,

contentType: 'json',

success: function () {

alert("succes");

},

error: function () {

alert("error");

}

});

在调试DataPost时填充。

调节器

[HttpPost]

public ActionResult UpdateJson(string id, string val)

{

//do stuff

return Json(true);

}

我在控制器中使用的参数与我的Ajax函数中的名称相同。 传递的格式是json,我也试过填充我的数据:

var dataPost = { 'id': 'id', 'val': 'val' };

但这没有任何区别。 我也尝试过使用类,比如 - >

public class ScheduleData

{

public string id { get; set; }

public string val { get; set; }

}

调节器

public ActionResult UpdateJson(ScheduleData data)

{//Do something}

任何帮助,将不胜感激。 提前致谢

I have looked over the net to figure out what my mistake is. All suggestions I found I tried, without any succes. I access the httppost action in my controller but the parameters stays empty.

AJAX function

var dataPost = { 'id': id, 'val': val };

debugger;

$.ajax({

type: 'POST',

url: '/Extensions/UpdateJson',

data: dataPost ,

contentType: 'json',

success: function () {

alert("succes");

},

error: function () {

alert("error");

}

});

On debug DataPost is populated.

Controller

[HttpPost]

public ActionResult UpdateJson(string id, string val)

{

//do stuff

return Json(true);

}

The parameters I used in my controller have the same name as in my Ajax function. The format passed is json, I have also tried populating my data with:

var dataPost = { 'id': 'id', 'val': 'val' };

But this doesn't make any difference. I have also tried to work with a Class, like -->

Class

public class ScheduleData

{

public string id { get; set; }

public string val { get; set; }

}

Controller

public ActionResult UpdateJson(ScheduleData data)

{//Do something}

Any help would be appreciated. Thanks in advance

原文:https://stackoverflow.com/questions/16708949

更新时间:2020-01-16 22:30

最满意答案

传递的格式是json

一点都不。 您没有发送任何JSON。 你做的是

data: { 'id': id, 'val': val }

但正如文档清楚地解释这是使用$.param函数,而后者又使用application/x-www-form-urlencoded编码。

因此,从$ .ajax调用中删除此contentType: 'json'属性。

或者,如果你真的想发送JSON,那么这样做:

var dataPost = { 'id': id, 'val': val };

$.ajax({

type: 'POST',

url: '/Extensions/UpdateJson',

data: JSON.stringify(dataPost),

contentType: 'application/json',

success: function () {

alert("succes");

},

error: function () {

alert("error");

}

});

需要注意的事项:

使用JSON.stringify(dataPost)来确保您向服务器发送JSON字符串

contentType: 'application/json'因为这是正确的Content-Type值。

The format passed is json

No, not at all. You are not sending any JSON. What you do is

data: { 'id': id, 'val': val }

But as the documentation clearly explains this is using the $.param function which in turn uses application/x-www-form-urlencoded encoding.

So get rid of this contentType: 'json' property from your $.ajax call.

Or if you really wanna send JSON, then do so:

var dataPost = { 'id': id, 'val': val };

$.ajax({

type: 'POST',

url: '/Extensions/UpdateJson',

data: JSON.stringify(dataPost),

contentType: 'application/json',

success: function () {

alert("succes");

},

error: function () {

alert("error");

}

});

Things to notice:

usage of JSON.stringify(dataPost) to ensure that you are sending a JSON string to the server

contentType: 'application/json' because that's the correct Content-Type value.

相关问答

似乎您没有在有效负载中传递完全相同的对象。 假设你的模型对象是这样的: {

foo: 1,

bar: 2

}

在前两个示例中,您将创建此对象: {

model: {

foo: 1,

bar: 2

},

name: "whatever"

}

因此,当您将其作为参数传递给ajax调用时,您传递的是MVC端不期望的对象。 如果要将其存储在变量中,则需要执行以下操作: var data = ko.toJS

...

我不认为你可以像你想要的那样绑定到动态类型。 您可以尝试创建映射数据的类,如下所示: public class Content

{

public string Name { get; set; }

public string Value { get; set; }

}

现在在你的行动中: [HttpPost]

public ActionResult NewService(Content[] data)

{

// sweet !

}

在你的js里,像Olaf Dietsc

...

您正在将您的JSON数据作为application/x-www-form-urlencoded提交,而应将其作为实际JSON提交: application/json

这可以很简单地完成,但.serializeArray(); 方法不会为原始JSON对象正确地解包。 相反,您可以使用下面的.serializeObject()方法: Javascript修复: 将ContentType设置为JSON 使用serializeObject()正确创建JSON对象 全部完成如下: jsFiddle演示 v

...

我只是尝试了以下它,它的工作原理,奖品绑定只是找到。 var prizeArr = [];

for (var i = 0; i <= 2; i++)

{

prizeArr.push({ Name:"test", LastName: "5", Age: "8" });

}

var myObj = {

SomeData: "This is a value",

Prizes: prizeArr

};

$.ajax({

type:

...

传递的格式是json 一点都不。 您没有发送任何JSON。 你做的是 data: { 'id': id, 'val': val }

但正如文档清楚地解释这是使用$.param函数,而后者又使用application/x-www-form-urlencoded编码。 因此,从$ .ajax调用中删除此contentType: 'json'属性。 或者,如果你真的想发送JSON,那么这样做: var dataPost = { 'id': id, 'val': val };

$.ajax({

...

data需要是一个Javascript对象文字: $.ajax({

type: "GET",

data: {ProjectID: p},

url: "/Home/Batches",

success: function(msg) {

populateBatches(msg);

}

});

data needs to be a Javascript object literal: $.ajax({

type: "GET",

da

...

有时候POCO解串器会因为奇怪的原因而被抓住。 我之前看到过我的JSON对象与POCO完全匹配,但它仍然不会反序列化。 发生这种情况时,我通常将对象作为JSON字符串发送到服务器,然后在服务器上反序列化它。 我个人使用ServiceStack.Text,因为它是最快的。 所以你的jQuery会像这样: var license = {

City: "New York",

CompanyID: 1,

County: "N/A",

IsActive: true

};

v

...

由于JS和C#中的参数名称不同,因此无法绑定: data: { createSammantrade: sammantrade, createStartTime: startTime, createEndTime: endTime, createLokal: lokal }

public ActionResult CreateEvent(string createSammantrade, string createStartTime, string createEndTime, int Lokal

...

您可以附加参数,如: var targeturl = '@Url.Action("Test", "Controller")?id=' + ID;

$.ajax({

url: targeturl,

type: "GET",

success: function(data) { },

error: function (data) { }

});

或者您可以直接使用jQuery ajax数据参数: $.ajax({

type: "POST",

url: '@Ur

...

那不行。 首先让我们澄清@RequestBody和@RequestParam之间的区别。 @RequestBody方法参数注释应该使用HttpMessageConverter将HTTP请求主体中的json值绑定到java对象。 HttpMessageConverter负责将HTTP请求消息转换为已声明的java对象。 资源 并使用@RequestParam注释将请求参数绑定到控制器中的方法参数。 资源 来找你问...用第一个ajax请求你发送JSON给你的控制器没有请求参数,所以@RequestB

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值