如果视图是从不同的控制器创建的,您可以从控制器调用方法吗?
假设我从返回视图 Index.aspx 的 HomeController 调用 Index(),Index.aspx 上的 javascript 中的 jquery ajax 可以调用另一个控制器(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;
没有给我任何错误,该页面是从 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;
给了我一个内部服务器错误...
只是想知道这是否可能是因为我调用的控制器与渲染视图的控制器不同
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用与呈现当前视图的控制器不同的控制器应该没有任何理由会导致任何类型的错误。 控制器只是一组通用命令的处理程序。 这些命令是由浏览器的地址栏、超链接还是 jQuery ajax 调用发出并不重要。 所有这三个都使用相同的协议向控制器发出命令。
我的猜测是您的代码有问题导致内部服务器错误。 您是否尝试过直接在浏览器中访问 /Row/getRowName 看看会发生什么? 它可以与 GET 和 POST 一起使用吗?只能使用 POST,只能使用 GET 吗? 也许您在仅支持 GET 的情况下发出 POST ? 在为您提供进一步帮助之前,我需要更多信息。
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.