0

I have the following chtml code to an action in a controller. The data is being sent by ajax to the action. The chtml part:

<li>
    <button id="abandon-popup" onclick="Transfer(@Model.Id)">
       Transfer <i class="icon-arrow-right"></i>
    </button>
</li>

The function Transfer:

function Transfer(modelId) {
    //alert(modelId);
    $.ajax({

        type: "GET",
        url: "/Internet/Transfer",
        data: "id=" + modelId,
        success: function (responsedata) {
            alert("ok");
            window.location.href = responsedata.newUrl;
        },
        error: function (data) {
          console.log("KO");
        }
    })
}

The action in the controller:

public ActionResult Transfer(long id)
{
  *some actions*
  return Json(new { newUrl = PartialView("~/Views/Shared/Partials/Leads/_TransferPopup.cshtml", commonModel) });
}

However I am getting a 500 internal error on this:

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

Any idea how to correct this?

refresh
  • 1,319
  • 2
  • 20
  • 71

2 Answers2

0

Try this following:

return Json(new { newUrl = PartialView("~/Views/Shared/Partials/Leads/_TransferPopup.cshtml", commonModel) });

into

return Json(new { newUrl = PartialView("~/Views/Shared/Partials/Leads/_TransferPopup.cshtml", commonModel) }, JsonRequestBehavior.AllowGet);

Change GET method to POST method the following way:

Client side:

function Transfer(modelId) {
//alert(modelId);
$.ajax({

    type: "POST",
    url: "/Internet/Transfer",
    data: {id: modelId},
    success: function (responsedata) {
        alert("ok");
        window.location.href = responsedata.newUrl;
    },
    error: function (data) {
      console.log("KO");
    }
})
}

Controller side:

[HttpPost]
public ActionResult Transfer(long id)
{
  *some actions*
  return Json(new { newUrl = PartialView("~/Views/Shared/Partials/Leads/_TransferPopup.cshtml", commonModel) });
}
Maths RkBala
  • 2,207
  • 3
  • 18
  • 21
0

Use this

  return Json(new { newUrl = PartialView("~/Views/Shared/Partials/Leads/_TransferPopup.cshtml",
                             commonModel
                 )}, JsonRequestBehavior.AllowGet);

By default, the ASP.NET MVC framework does not allow you to respond to an HTTP GET request with a JSON payload. If you need to send JSON in response to a GET, you'll need to explicitly allow the behavior by using JsonRequestBehavior.AllowGet as the second parameter to the Json method. However, there is a chance a malicious user can gain access to the JSON payload through a process known as JSON Hijacking. You do not want to return sensitive information using JSON in a GET request.

Saurin Vala
  • 1,898
  • 1
  • 17
  • 23