weixin_33709219 2017-08-10 15:41 采纳率: 0%
浏览 29

ASP MVC 6 AJAX错误请求

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.

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). I've tried every combination of slashes before and after words in the URL syntax. I've even tried using the @Url.Action method, which gave me that string anyway, so I know it's correct.

I don't know what else to try. Does anyone know what is going on?

  • 写回答

1条回答 默认 最新

  • ~Onlooker 2017-08-10 15:46
    关注

    I think there is a problem with the ValidateAntiForgeryToken you've used in you controller as a filter. You have to send the related input value with your data to the server. Try to remove this filter.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)