1

I have been messing around with MVC6 controllers and action methods. Something I noticed was that returning JSON results are different in that there is no JsonRequestBehaviour anymore. Ajax Posts are working fine but I have a Get that is consistently returning a 502 (Bad Gateway). The server action method is working correctly.

 $.ajax({
            type: "GET",
            url: "/managingagent/loadagents",
            data: "managingagentid=" + @Model.ManagingAgentId,
            dataType: "json",
            success: function(response) {
                alert("SUCCESS");
            },

            fail: function(response) {
                alert("ERROR");
            }
        });

Action Method

 [HttpGet]
    public IActionResult LoadAgents(int managingAgentId)
    {
        var userId = User.GetUserId();
        var managingAgent =
            _context.ManagingAgentMember.Where(x => x.ApplicationUserId == int.Parse(userId))
                .Select(x => x.ManagingAgent).Single();

        var agents = _context.ManagingAgentMember.Where(x => x.ManagingAgentId == managingAgent.ManagingAgentId && x.IsActive).ToList();
        return Json(agents);
    }
Greg
  • 2,654
  • 3
  • 36
  • 59
  • Cant duplicate your issue but what is preventing you from using POST? – JamieD77 Jan 13 '16 at 19:48
  • I tried using POST and get the same result. However, I have another POST method that is saving data correctly, however, in that method I am just returning a confirmation message json string. – Greg Jan 13 '16 at 20:04
  • Have you tried setting the contentType of the ajax call? – JB06 Jan 13 '16 at 21:43

3 Answers3

1

To resolve it, add the proxy_buffer_size configurations to your location block.

server {
    listen        80;
    server_name   host.tld;

    location / {
          proxy_pass       http://upstream;
          proxy_buffer_size          128k;
          proxy_buffers              4 256k;
          proxy_busy_buffers_size    256k;
    }
}
wiloke
  • 11
  • 1
0

It is nothing to do with GET or POST. The data in the ajax call("managingagentid=" + @Model.ManagingAgentId) is a string instead of JSON. You can use JSON.stringify({ managingagentid: @Model.ManagingAgentId}); to convert it to a JSON data.
Actually, since you are sending a single parameter, I would suggest you to pass it along with the URL.

$.ajax({
            type: "GET",
            url: "/managingagent/loadagents/"+ @Model.ManagingAgentId,
            dataType: "json",
            success: function(response) {
                alert("SUCCESS");
            },

            fail: function(response) {
                alert("ERROR");
            }
        });

Note: Since you are using the razor C# expression @.. to get the model values, just make sure that they are getting evaluated properly.

Pradeep Kumar
  • 1,281
  • 7
  • 9
-1

I think you need to change your code in someways. Change you jQuery as:

        type: "GET",
        url: "/managingagent/loadagents",
        dataType: "JSON",
        data: { managingAgentId: @Model.ManagingAgentId },

Also change last line of your action code to :

return Json(agents, JsonRequestBehavior.AllowGet);
Hadee
  • 1,392
  • 1
  • 14
  • 25
  • I thought I did explain clearly that JsonRequestBehaviour does not exist in MVC6 and that the action method is being called correctly. – Greg Jan 13 '16 at 20:03