views:

92

answers:

1

Say I called Index() from the HomeController which returns the view Index.aspx, can jquery ajax in javascript on Index.aspx call a method in another controller (ExternalController)?

$.ajax({
            type: "POST",
            url: "/Home/sayHi",
            dataType: "json",
            data:({ currRow : centerPost[0], offset : ((-1 * Math.ceil(numRows / 2)) + 1) }),
            error:function(request){alert(request.statusText)},
            success:function(result){alert(result)}
        }).responseText;

gave me no error, the page was rendered from the HomeController

$.ajax({
            type: "POST",
            url: "/Row/getRowName",
            dataType: "json",
            data:({ currRow : centerPost[0], offset : ((-1 * Math.ceil(numRows / 2)) + 1) }),
            error:function(request){alert(request.statusText)},
            success:function(result){alert(result)}
        }).responseText;

gave me an Internal Server Error...

just wondering if it could be because I'm calling a different Controller than the one the View was rendered from

+2  A: 

There shouldn't be any reason why calling a different controller than the one that rendered your current view would result in any kind of errors. A controller is just a handler of a common set of commands. Whether those commands are issued by a browser's address bar, by a hyperlink, or by a jQuery ajax call doesn't matter. All three use the same protocol to issue commands to the controller.

My guess is that there is something wrong with your code that is causing the Internal Server Error. Have you tried going to /Row/getRowName directly in the browser to see what happens? Does it work with GET and POST, only POST, only GET? Perhapse you are issuing a POST when only GET is supported? I would need more information before I could help you further.

jrista