为了账号安全,请及时绑定邮箱和手机立即绑定

.net mvc Ajax Post 错误 403 Forbidden

.net mvc Ajax Post 错误 403 Forbidden

C#
元芳怎么了 2021-07-02 10:07:49
我正在执行一个简单的函数来更新数据库中的字段,但出现此错误:我在 html/Jquery 中执行请求:function AgregarLike(id, num){        alert("Entre:" + id);        var urlAction = "@Url.Action("UpdateLikeVisitBrandPhoto", "Report")";        alert (urlAction);        var request;        // Fire off the request to /form.php        request = $.ajax({        url: urlAction + '/' + id,        type: "post"        });       // Callback handler that will be called on success       request.done(function (response, textStatus, jqXHR){       // Log a message to the console       console.log("Hooray, it worked!");       console.log(response);       console.log(textStatus)       alert("worked");       });}和控制器(我一直返回 bu.CreateLike(Id) 因为我想强制错误):   public int UpdateLikeVisitBrandPhoto(int id)    {        try        {            try            {               var num = bu.CreateLike(id);            }            catch            {                return bu.CreateLike(id);            }            return bu.CreateLike(id);        }        catch (ServicesException ex)        {            logger.Error("", ex);            Console.WriteLine(ex);            return bu.CreateLike(id);        }        catch (Exception ex)        {            logger.Error("", ex);            Console.WriteLine(ex);            return bu.CreateLike(id);        }    }和模型: public int CreateLike(int id)    {        using (var sqlConnection = DatabaseUtilities.GetConnection())        {            var SQL = "UPDATE [RBAcuerdos].[dbo].[VisitBrandPhoto] SET MeGusta = 1 WHERE id = @paramId";            var sqlCommand = new SqlCommand(SQL, sqlConnection);            sqlCommand.Parameters.Add(new SqlParameter("paramId", id));            //sqlCommand.Parameters.Add(new SqlParameter("paramvalue", 1));            return sqlCommand.ExecuteNonQuery();        }        //throw new NotImplementedException();    }有人可以帮助我吗?
查看完整描述

2 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

request = $.ajax({

        url: urlAction + '?id=' + id,

        type: "get"

        });

替换您的代码


var urlAction = "@Url.Action("UpdateLikeVisitBrandPhoto", "Report")";

它产生


/Report/UpdateLikeVisitBrandPhoto

要点击控制器,你需要你的网址


/Controller/Action?param1=paramvalue //single param

/Controller/Action?param1=paramvalue &param2=paramvalue //multiple params,apppend each paramname with prefix &


查看完整回答
反对 回复 2021-07-03
?
守着星空守着你

TA贡献1799条经验 获得超8个赞

由于您正在发送 POST 请求,因此您需要发送的参数不应是 URL 的一部分。尝试发送如下参数:


 request = $.ajax({

    url: urlAction,

    data: {id: id},

    type: "POST",

    contentType: 'application/json; charset=utf-8',

    success: function (data) {

       alert("It worked!");

    },

    error: function () {

       alert("Error");

    }

 });


查看完整回答
反对 回复 2021-07-03
  • 2 回答
  • 0 关注
  • 248 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信