.net mvc Ajax Post 错误 403 Forbidden

我正在执行一个简单的函数来更新数据库中的字段,但出现此错误:

http://img2.mukewang.com/60e00e490001423312750103.jpg

我在 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();

    }

有人可以帮助我吗?


元芳怎么了
浏览 250回答 2
2回答

SMILET

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 &

守着星空守着你

由于您正在发送 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");    } });
打开App,查看更多内容
随时随地看视频慕课网APP