jQuery.post与MVC
我对jQuery.post与MVC,jquery,asp.net-mvc,post,Jquery,Asp.net Mvc,Post,我对jQuery.post()语法有一个问题 我有控制器ExampleController.cs,带有一个动作: public ActionResult TestPost(Guid fileId) { //do something } 和视图Example.cshtml,在这里我尝试加载此操作: function foo(fileGuid) { $.post("Example/TestPost?fileId=" + fileGuid, function () {
jQuery.post()
语法有一个问题
我有控制器ExampleController.cs
,带有一个动作:
public ActionResult TestPost(Guid fileId) { //do something }
和视图Example.cshtml
,在这里我尝试加载此操作:
function foo(fileGuid) {
$.post("Example/TestPost?fileId=" + fileGuid, function () {
alert("success");
});
}
但是没有警告,我认为这个语法有问题。有人能帮我吗。谢谢。尝试添加以下3个回调,看看是否收到错误警报
var jqxhr = $.post("example.php", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
您还应该检查Chrome控制台中的网络选项,看看服务器端是否有任何错误。这也可能是函数失败的原因。我认为您需要在位置上添加反斜杠:
$.post("/Example...
如果不起作用,请尝试使用后期注释对方法进行注释:
[HttpPost]
public ActionResult TestPost(Guid fileId) { //do something }
编辑:
它找不到您的操作,因为您正在将参数指定为Guid。如果将其更改为字符串:
public ActionResult TestPost(string fileId)
{
return View();
}
并运行:
$.post('/Example/TestPost?fileId=123', function () {
alert('here');
});
它应该会工作的好的,我发现这样做:
$.post('@Url.Action("TestPost", "Example")', {fileId : fileGuid} , function () {
alert("success");
});
而且有效。:) 嗨,谢谢。我照你的建议做了。我得到了两个警报错误和完成,错误是example.php没有找到。顺便说一句,这不是php。@QuiTran请接受答案并关闭进一步讨论的问题。我检查了它,它说POST TestPost?fileId=blahblah 404未找到。我认为它无法从服务器加载数据???我认为您应该检查是否从ajax函数传递了正确的url。