如何从JQuery调用控制器方法? [英] How do I call a controller method from JQuery?

查看:403
本文介绍了如何从JQuery调用控制器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net(非mvc)的ajax代码,可以调用webMethod从服务器获取其他数据以进行请求.但是我似乎无法弄清楚在MVC中提供我的JQuery的网址.

I have ajax code for asp.net (non-mvc) to call to a webMethod to get additional data from the server for a request. But I can't seem to figure out the url to give my JQuery in MVC.

<script type="text/javascript" language="javascript">
function SubmitAjax(url, message, successFunc, errorFunc) {
    $.ajax({
        type:"POST",
        url:url,
        data:message,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success:successFunc,
        error:errorFunc
        });

};

我不想将相关数据的整个列表传递给选择列表,以到达页面的每个人,因为并非所有人都需要此功能.因此,我想通过JQuery调用控制器或网络方法,但似乎无法找出如何在MVC中寻址URL.

I don't want to pass the entire list of related data to a select list to every person that arrives at the page, as not all will need this functionality. So I'd like to call a controller or webmethod via JQuery, but can't seem to find out how to address the URL in MVC.

我注意到了这篇文章: 将jQuery Ajax调用解析为当前的控制器文件夹,而不是根文件夹

I noticed this post: JQuery Ajax call gets resolved to the current Controller Folder, Instead of root Folder

$.getJson是mvc方法吗?这对我的用例来说是一个很好的解决方案吗?我只需要返回一个字符串url,或者如果找不到我要寻找的内容,则返回一个空字符串.我是否需要为$ .getJSon方法添加一个?是MVC的一部分还是JQuery的一部分?前导斜线是指向应用程序根目录还是服务器根目录?

is $.getJson an mvc method? is this a good solution for my use case? I only need to return a string url, or an empty string if what i'm looking for is not found. Do I need to include a for the $.getJSon method? is that part of MVC or part of JQuery? Is the leading slash going to point to Application root or server root?

推荐答案

问题是控制器上的webMethod标记,在asp.net中是必需的或有用的,而在asp.net mvc中则不是

The problem was the webMethod Tag on the controller, that was needed or helpful in asp.net, but not in asp.net mvc

这篇关于如何从JQuery调用控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆