简体   繁体   English

ASP MVC 6 AJAX错误请求

[英]ASP MVC 6 AJAX Bad Request

My intention is to update a database without completely reloading the current page the user is on when the user submits the data, instead just popping a message box saying success or failure, or something like that. 我的意图是更新数据库,而无需在用户提交数据时完全重新加载用户所在的当前页面,而只是弹出一个提示成功或失败或类似信息的消息框。

I have the following JS function which is called when the user clicks a button. 我有以下JS函数,当用户单击按钮时会调用该函数。

function saveListItem(formID)
{
    alert('JS function fired.');
    var _form = $('#mainForm' + formID)
    var _formData = _form.serialize();

    $.ajax({
        dataType: "json",
        type: "POST",
        url: "/Postings/listItemSubmitted",
        contentType: "application/json; charset=utf-8",
        data: { type: "test" },
        success: function () {
            alert("success");
        },
        error: function (xhr, status, error)
        {
            alert(error.toString());
        }
    });
}

And here is the controller action: 这是控制器动作:

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult listItemSubmitted([FromBody] string type)
{
    type += "!!!";
    return Json(type);
}

As you can see, I have a few variables that I later intend to use to actually do something. 如您所见,我有一些变量供以后实际使用。 But for now, I'm just trying to get just this TEST to work. 但就目前而言,我只是试图使此测试起作用。 The problem is that the action in the controller will not even fire. 问题在于控制器中的动作甚至不会触发。 The error alert gives me "Bad Request." 错误警告提示我“错误请求”。

I've checked the names, all of the names are correct(controller is PostingsController.cs, method name is listItemSubmitted). 我检查了名称,所有名称都是正确的(控制器是PostingsController.cs,方法名称是listItemSubmitted)。 I've tried every combination of slashes before and after words in the URL syntax. 我已经尝试了URL语法中单词前后的斜杠的每种组合。 I've even tried using the @Url.Action method, which gave me that string anyway, so I know it's correct. 我什至尝试使用@ Url.Action方法,无论如何都会给我该字符串,所以我知道它是正确的。

I don't know what else to try. 我不知道还能尝试什么。 Does anyone know what is going on? 有人知道发生了什么吗?

I think there is a problem with the ValidateAntiForgeryToken you've used in you controller as a filter. 我认为您在控制器中用作过滤器的ValidateAntiForgeryToken存在问题。 You have to send the related input value with your data to the server. 您必须将相关的输入值和数据发送到服务器。 Try to remove this filter. 尝试删除此过滤器。

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

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