简体   繁体   English

.net mvc Ajax发布错误403禁止

[英].net mvc Ajax Post Error 403 Forbidden

I am doing a simple function to update a field in the database and I get this error: 我正在执行一个简单的功能来更新数据库中的字段,但出现此错误:

Failed to load resource: the server responded with a status of 403 (Forbidden) 加载资源失败:服务器响应状态为403(禁止)

I do the request in html/Jquery: 我在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");
       });

}

And the controller (I return all the time bu.CreateLike(Id) because I want to forze the error): 和控制器(我一直返回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);
        }


    }

And the model: 和模型:

 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();
    }

Someone can help me please? 有人可以帮我吗?

Since you're sending a POST request, the parameters you need to send should not be a part of the URL. 由于您正在发送POST请求,因此您需要发送的参数不应成为URL的一部分。 Try sending the parameters like: 尝试发送如下参数:

 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");
    }
 });

Further Reading: How to pass parameters in $ajax POST? 进一步阅读: 如何在$ ajax POST中传递参数?

request = $.ajax({
        url: urlAction + '?id=' + id,
        type: "get"
        });

Substitute your code 替换您的代码

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

It generates 它产生

/Report/UpdateLikeVisitBrandPhoto

To hit controller you need your url to be 要点击控制器,您需要输入网址

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM