MVC 中的 Jquery ajax 调用
我使用一个自制的框架...我想用 jquery 进行 ajax 调用,但我不知道如何传递 url...我的意思是如果我有控制器“类别”和操作“索引”ajax 调用会是这样吗?
$.ajax({
type: "POST",
url: "http://localhost/learning/categories/index/",
});
I work with a self made framework...I want to make an ajax call with jquery and I don't know how to pass the url...I mean if i have controller "categories" and action "index" the ajax call would be like this?
$.ajax({
type: "POST",
url: "http://localhost/learning/categories/index/",
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个怎么样?
How about this?
您的 JavaScript 是否位于 ASP.NET MVC 视图中?如果是这样,您可以使用 Url.Action 动态生成 URL。
如果您的 JavaScript 位于单独的 .js 文件中,那么您就增加了一点复杂性。
我的解决方案是动态渲染 .js 文件(即,将 script.js 路由到返回内容类型为 text/javascript 的视图的操作方法)。
我还尝试将 URL 从渲染视图传递到 .js 文件,但这似乎比上面的解决方案更混乱。
在周二的 MVC Conf 中,建议您避免同时支持相对和绝对 URL - 这样您就可以对基本 URL 做出假设。这也有效...
Is your JavaScript in an ASP.NET MVC view? If so, you can use Url.Action to generate the URL on the fly.
If your JavaScript is in a separate .js file, then you've added a bit more complexity.
My solution was to render the .js file dynamically (i.e. route scripts.js to an action method that returns a view with a contenttype of text/javascript).
I've also tried passing the URL from the rendered view to the .js file but this seemed messier than the above solution.
In MVC Conf on Tuesday, it was suggested that you simply avoid supporting both relative and absolute URLs - thus you could make assumptions about the base URL. That works too...