亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

.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 關注
  • 298 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號