.net mvc Ajax发布错误403禁止 [英] .net mvc Ajax Post Error 403 Forbidden

查看:115
本文介绍了.net mvc Ajax发布错误403禁止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am doing a simple function to update a field in the database and I get this error:

无法加载资源:服务器的响应状态为403(禁止)

我在html/Jquery中执行请求:

I do the request in 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),因为我想伪造错误):

And the controller (I return all the time bu.CreateLike(Id) because I want to forze the error):

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

有人可以帮我吗?

推荐答案

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

替换您的代码

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

它生成

/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 &

这篇关于.net mvc Ajax发布错误403禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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