1

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)

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

}

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


    }

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?

Javi
  • 23
  • 1
  • 8

2 Answers2

0

Since you're sending a POST request, the parameters you need to send should not be a part of the 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?

Mikaal Anwar
  • 1,720
  • 10
  • 21
  • This doesnt resolve my problem. I get the same message: /Acuerdos/Report/UpdateLikeVisitBrandPhoto Failed to load resource: the server responded with a status of 403 (Forbidden) – Javi May 24 '18 at 17:02
  • I believe this has to do with authentication and not with the semantics of sending a POST request. Please check out the following: https://stackoverflow.com/questions/2805311/403-forbidden-access-is-denied-asp-net-mvc – Mikaal Anwar May 24 '18 at 17:06
  • This doesnt work neither. The same error again: POST https://rbhealth.es/Acuerdos/Report/UpdateLikeVisitBrandPhoto 403 (Forbidden) – Javi May 24 '18 at 17:09
  • Its not this solution neither ==> https://stackoverflow.com/questions/2805311/403-forbidden-access-is-denied-asp-net-mvc. – Javi May 24 '18 at 17:10
  • @Javi Have you tried solution # 2 here (https://stackoverflow.com/questions/2805311/403-forbidden-access-is-denied-asp-net-mvc)? Regarding Network Service permissions? – Mikaal Anwar May 24 '18 at 17:14
  • Yes, I have my web.config like this, at least in that instructions. – Javi May 24 '18 at 17:22
0
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 &
Sumit raj
  • 821
  • 1
  • 7
  • 14