Kianoush
Kianoush

Reputation: 39

' set JsonRequestBehavior ' in ASP.Net MVC5

I Creating a News Site With ASP.Net MVC5 . Every News has a Like And DisLike .

i Using Ajax and Json for Ban Refresh The Page .

But When i Clicked in Like Or DisLike Show Me This Error :

 This request has been blocked because sensitive information could be 

disclosed to third party web sites when this is used in a GET request. To allow

 GET requests, set JsonRequestBehavior to AllowGet.

HomeController :

public ActionResult NewsLike(int ID)
        {
           int Like=RNews.Like(ID);
           return Json(Like);
            }

Rep_New :

  public int Like(int ID)
    {
        var qLike = (from a in db.Tbl_News
                     where a.ID.Equals(ID)
                     select a).SingleOrDefault();
        try
        {
            qLike.Like++;
            db.Tbl_News.Attach(qLike);
            db.Entry(qLike).State = System.Data.Entity.EntityState.Modified;
            if (db.SaveChanges() > 0)
            {
                return qLike.Like;
            }
            else
            {
                return qLike.Like--;
            }
        }
        catch (Exception)
        {
            return qLike.Like;
        }
    }

News.cshtml :

<table style="margin-top:10px;">
                    <tr style="color:green;font-size:12px;margin-top:10px;">
                        <td>Like :</td>
                        <td><span id="Like@(Counter)">@item.Dislike</span></td>
                        <td>@Ajax.ActionLink("+", "CommentLike", new { ID = item.ID }, new AjaxOptions { HttpMethod="POST",UpdateTargetId="Like"+Counter})</td>
                    </tr>
                    <tr style="color:red;font-size:12px;margin-top:10px;">
                        <td>DisLike :</td>
                        <td><span id="DisLike@(Counter)">@item.Dislike</span></td>
                        <td>@Ajax.ActionLink("+", "CommentDisLike", new { ID = item.ID }, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "DisLike" + Counter })</td>
                    </tr>
                </table>

Upvotes: 0

Views: 3954

Answers (2)

mmushtaq
mmushtaq

Reputation: 3520

public JsonResult NewsLike(int ID)
{
    int Like=RNews.Like(ID);
    return Json(Like, JsonRequestBehavior.AllowGet);
}

you have to provide JsonRequestBehavior to return Json result to view.

Note: Json request Get behavior and HTTP Get request are not same.

Upvotes: 3

MichaelMao
MichaelMao

Reputation: 2796

return Json(result, JsonRequestBehavior.AllowGet);

Update, try this

public ActionResult NewsLike(int ID)
{
   int Like=RNews.Like(ID);
    return Json(Like, JsonRequestBehavior.AllowGet);
}

Upvotes: 1

Related Questions