javascript位置href不能在jquery ajax帖子上工作

时间:2013-08-13 10:33:39

标签: javascript jquery asp.net-mvc

我有以下问题:我创建了一个jQuery ajax帖子,并且在成功之后,我需要将浏览器的位置更改为Index视图。 这是javascript:

$.post(CreateReleaseNotificationURL, Notifications_Form.serialize(), function (response) {
            if (response.indexOf("Error") == 0) {
                $("#NewNotification_CreateStatus").html(response);
            }
            else {
                window.location.assign('@Url.Action("Index")');
                //window.location = window.location;                   
            }              
        })
        .fail(function () {                
            $("#NewNotification_CreateStatus").html("An error has occured.<br /> Please contact the system administrator<br /> if the problem persists.");
        });

然而,没有任何反应!!我错过了什么?

PS:我也试过使用location.href,但没有用。

2 个答案:

答案 0 :(得分:0)

检查一下:

window.location.href = '@Url.Action("Index", "ControllerName")';

答案 1 :(得分:0)

在此处发布(Mixing razor syntax with Javascript in views),应该是:

在View文件中:

<input type="button" value="Resume" id="myButton" data-url='@Url.Action("Index")' />

在javascript文件中(根据您的需要进行调整):

$(function() {
    $('#myButton').click(function() {
        window.location.href = $(this).data('url');
    });
});