所以我有一个基本的控制器接受一个帖子。
[HttpPost]
public ActionResult Submit(string postCost)
{
//Do stuff here before sending back redirect details...
return Json(new { result = "Redirect", url = Url.Action("Index", "Confirm") });
}
我通过jQuery ajax方法发布:
$.ajax({
url: partyURL,
dataType: 'json',
contentType: 'application/json', //charset=utf-8',
type: 'POST',
data: { postCost: postageCost}, //**This fails!**
//data: "{'postCost':'3.50'}", //**This works**
success: function (response) {
if (response.result == 'SoldOut') {
$("#soldOut").show();
}
else if (response.result == 'Redirect') {
//All good, onward to confirmation page
window.location = response.url;
}
},
error: function (xhr, status, error) {
// Error handling here
}
});
当postageCost变量返回失败状态500时发送的位置:
postageCost = '3.50';
postageCost = JSON.stringify(postageCost); //also fails with this
但是如果我将数据定义硬编码为
data: "{'postCost':'3.50'}",
它工作正常。
因此,关键必须在于我在处理数据元素吗?
最佳答案
你需要做
var datum = {'postCost': '3.50'};
data: JSON.stringify(datum), //Ajax call data
关于javascript - MVC jQuery Ajax Post-只能使其与硬编码值一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30098628/