简体   繁体   English

用jQuery发布在Asp.Net MVC中不起作用?

[英]Post with jQuery not working in Asp.Net MVC?

I'm trying to make a post call to an action method in MVC 2. But it doesn't seem to work. 我正在尝试对MVC 2中的操作方法进行后期调用。但这似乎不起作用。 I have set a break point right at the beginning of the action method, but the debugger never hits the breakpoint. 我已经在action方法的开始处设置了一个断点,但是调试器从不打断点。 What am I doing wrong? 我究竟做错了什么? Here's the jQuery (for simplicity I have simplified it by removing parameters both from the call and the action method). 这是jQuery(为简单起见,我通过从call和action方法中删除参数来简化了它)。 Please note that the callback alert does get called, which makes it even stranger. 请注意,回调警报确实会被调用,这使其变得更加陌生。

$("#deleteFile").click(function () {
    var url = '<%= Url.Action("DeleteFile", "Customers") %>';

    $.post(url,
    null,
    function (data) {
        alert("POSTING TO ACTION METHOD");
    }
    );

});

The action method signature looks like this: 动作方法签名如下所示:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult DeleteFile()
{ ... etc }

Also, other jQuery calls have worked against the action methods, with or without parameters, eg the .load function. 另外,其他jQuery调用都针对具有或不带有参数(例如.load函数)的action方法。

Ah, never mind, I found the problem myself. 啊,没关系,我自己发现了问题。 The element with the id deleteFile was a link, and I had not used event.preventDefault(); 标识为deleteFile的元素是一个链接,并且我没有使用event.preventDefault();。 so there was an empty href, which made the page reload instead of posting via jQuery. 因此有一个空的href,它使页面重新加载而不是通过jQuery发布。 Once I added that it worked :-) 一旦我添加了它就可以了:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM