Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax post请求未通过ID_Javascript_Html_Ajax_Asp.net Mvc 5_Http Post - Fatal编程技术网

Javascript Ajax post请求未通过ID

Javascript Ajax post请求未通过ID,javascript,html,ajax,asp.net-mvc-5,http-post,Javascript,Html,Ajax,Asp.net Mvc 5,Http Post,我目前正在通过javascript动态设置小部件上的数据属性,这些数据属性是ID,然后在我删除小部件时获取属性,以便从数据库中删除小部件条目。我已经在firebug中详细介绍了代码,它似乎很好地获得了widgetID,但是当我发出ajax post请求时,它似乎没有为路由值附加ID 以下是模式: div class="modal fade" id="deleteWidgetModal" tabindex="-1" role="dialog" aria-labelledby="myModalLab

我目前正在通过javascript动态设置小部件上的数据属性,这些数据属性是ID,然后在我删除小部件时获取属性,以便从数据库中删除小部件条目。我已经在firebug中详细介绍了代码,它似乎很好地获得了widgetID,但是当我发出ajax post请求时,它似乎没有为路由值附加ID

以下是模式:

div class="modal fade" id="deleteWidgetModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Delete widget?</h4><!--add depending on which panel you have clicked-->
        </div>
        <div class="modal-body" id="myModalBody">
            <!--Depending on which panel insert content-->
            @using (Html.BeginForm("DeleteWidgetConfirmed", "Dashboard", FormMethod.Post, new { id = "__AjaxAntiForgeryForm" }))
            {
                @Html.AntiForgeryToken();

                <div class="form-horizontal">
                    Do you wish to delete this widget?

                    <div class="form-group">
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                            <button type="submit" value="DeleteWidgetConfirmed" class="btn btn-danger btn-ok" id="delete-widget">Delete</button>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>
}))

这是我从firebug中的网络面板获得的HTTP POST请求:

/仪表板/删除已确认

这是我的控制器:

// POST: DashboardModels/Delete/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteWidgetConfirmed(int? id)
    {
        if(id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        DashboardModel dashboardModel = db.dashboards.Find(id);
        db.dashboards.Remove(dashboardModel);
        db.SaveChanges();
        return new EmptyResult();
    }
下面是随我的响应传递的参数:

评论不用于扩展讨论;此对话已结束。请查看firefox/google chrome以查看帖子正文中的json数据。验证它是否是有效的JSOn数据。
$(document).ready(function () {
$('#columns').on('click', '.glyphicon.glyphicon-trash', function (event) {
    var panel = this;
    //get id here

    //toggle the modal
    $('#deleteWidgetModal').modal('show');
    var widgetID = $(this).closest('.panel.panel-default').attr('data-widgetid');

    document.getElementById('delete-widget').onclick = function (event) {
        event.stopPropagation();

        //anti forgery token
        var form = $('#__AjaxAntiForgeryForm');
        var token = $('input[name="__RequestVerificationToken"]', form).val();

        var URL = '/Dashboard/DeleteWidgetConfirmed';

        console.log(widgetID + " test1");
        //we make an ajax call to the controller on click
        $.ajax({
            url: URL,
            data: {
                __RequestVerificationToken: token,
                id: widgetID   
            },
            type: 'POST',
            dataType: 'json',
            success: function(data){
                var parentElement = $(panel).closest(".col-md-4.column");
                var targetElement = $(panel).closest(".panel.panel-default");
                targetElement.remove();

                //parentElement.addClass("expand-panel");
                checkEmptyPanelContainers();
                $('#deleteWidgetModal').modal('hide');
            },
            error: function (response) {
                console.log(widgetID + " ERROR");
            }
        })           
    }
})
// POST: DashboardModels/Delete/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteWidgetConfirmed(int? id)
    {
        if(id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        DashboardModel dashboardModel = db.dashboards.Find(id);
        db.dashboards.Remove(dashboardModel);
        db.SaveChanges();
        return new EmptyResult();
    }