jQuery的发布得到了500内部服务器错误? [英] jQuery posting got 500 Internal Server Error?

查看:283
本文介绍了jQuery的发布得到了500内部服务器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的发帖事件是

 <脚本>
    $(文件)。就绪(函数(){
        $(#去)。点击(函数(){
            $。员额(/首页/ ToggleGoing
             {
                 去:$(#去)VAL()
             },
             功能(数据){
               //没有成功code。
            });
        });
    });
< / SCRIPT>

和控制动作

  [HttpPost]
    [ValidateAntiForgeryToken]
    公众的ActionResult ToggleGoing(布尔去,诠释?ID)
    {
        变种E = db.Events.FindAsync(ID);

不过,JavaScript控制台显示它得到500错误?

 POST https://开头本地主机:44300 /主页/ ToggleGoing 500(内部服务器错误)


解决方案

本: ValidateAntiForgeryToken

当您使用 $。员额()不被包含包含验证令牌的隐藏字段。你需要把它列入你的POST数据:

  $。员额(/首页/ ToggleGoing
    {
        去:$(#去)VAL()
        __RequestVerificationToken:$('输入[名称=__ RequestVerificationToken])VAL()。
    },功能(数据){});

当然,这是假定您已经添加了一个 @ Html.AntiForgeryToken()某处表单。

The jQuery posting event is

<script>
    $(document).ready(function () {
        $("#going").click(function () {
            $.post("/Home/ToggleGoing", 
             { 
                 going: $("#going").val() 
             }, 
             function (data) {
               //No success code.   
            });
        });
    });
</script>

And the action in control is

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ToggleGoing(bool going, int? id)
    {
        var e = db.Events.FindAsync(id);

However, the javascript console shows it got 500 error?

POST https://localhost:44300/Home/ToggleGoing 500 (Internal Server Error)

解决方案

This: ValidateAntiForgeryToken.

When you use $.post() the hidden field that contains the validation token is not being included. You need to include it in your POST data:

$.post("/Home/ToggleGoing", 
    { 
        going: $("#going").val(),
        __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val()
    }, function (data) {

});

Of course, this assumes that you've added a @Html.AntiForgeryToken() to your form somewhere.

这篇关于jQuery的发布得到了500内部服务器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆