简体   繁体   English

ASP.NET使用Jquery / AJAX执行WebMethod

[英]ASP.NET execute WebMethod with Jquery/AJAX

I have one WebMethod that will execute some DB search and return it's data in some HTML template. 我有一个WebMethod,它将执行一些数据库搜索并以某些HTML模板返回其数据。 I need to execute this method using jquery to populate an area of the website but the problem is that my website URL/URI is dynamic. 我需要使用jquery执行此方法来填充网站的某个区域,但是问题是我的网站URL / URI是动态的。

My URL is http://site/school-name/home . 我的网址是http://site/school-name/home The school-name will always change to indicate wich school i'm accessing. school-name将始终更改以指示我正在访问的学校。

I have accomplished so far: 到目前为止,我已经完成了:

$.ajax({
    type: "POST",
    url: "/Default.aspx/BuscaEquipe",
    data: { 'pIndex': pIndex, 'pLimite': 4, 'pUnidadeCE': codigoEmitente },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
        alert(response.d);
    },
    failure: function(response) {
        alert(response.d);
    }
});

and the WebMethod: 和WebMethod:

public static string BuscaEquipe(int pIndex, int pLimite, int pUnidadeCE)
{
    var objEquipe = new Equipe { EquipeUnidadeCE = pUnidadeCE, EquipeAtivo = 1 };
    var CaminhoImagem = Configuracoes.CaminhoVirtual + "Controles/Redimensiona.ashx?img=" + Configuracoes.CaminhoVirtual + "images/equipe/" + pUnidadeCE + "/";

    if (!objEquipe.Listar(objEquipe)) return "";

    var depoimentos = objEquipe.lstEquipe.Skip(pIndex).Take(pLimite);

    var objJson = new JavaScriptSerializer().Serialize(depoimentos.Aggregate("", (current, equipe) =>
            current + ("<div class='col-lg-3 col-md-3 col-sm-3'><img src='" + CaminhoImagem + equipe.EquipeImagem + "&w=400&h=400' alt='" + equipe.EquipeNome + "' class='img-circle img_perfil'><div class='nome_perfil text-center'>" + equipe.EquipeNome + "</div></div>")));

    return objJson;
}

Using like this i get a 401 Not Authorized and if i try to use my full URL http://site/school-name/Default.aspx/BuscaEquipe i get a 404. 像这样使用,我得到401未经授权,如果尝试使用我的完整URL http://site/school-name/Default.aspx/BuscaEquipe我得到404。

PS I have already used this same method in another project and it worked fine but i can't figure out what's wrong in this one, i think it might be related to the URl but i'm not sure. PS:我已经在另一个项目中使用了相同的方法,并且效果很好,但是我无法弄清楚这个问题出在哪里,我认为它可能与URl有关,但我不确定。

the problem is with your URL 问题出在您的网址上

Use the ResolveClientUrl() method 使用ResolveClientUrl()方法

<%= ResolveUrl("~/Default.aspx/BuscaEquipe") %>

And you must Have [WebMethod] attribute before your static server function 并且您必须具有[WebMethod]属性,然后才能使用静态服务器功能

[WebMethod]
public static string BuscaEquipe(int pIndex, int pLimite, int pUnidadeCE)
{
   //Code
}

Your data: 您的数据:

var requestData= JSON.stringify({ pIndex: pIndex, pLimite: 4, pUnidadeCE: codigoEmitente }) var requestData = JSON.stringify({pIndex:pIndex,pLimite:4,pUnidadeCE:codigoEmitente})

and then 接着

data:requestData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM