栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

C# Web method is not calling in javascript

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# Web method is not calling in javascript

There are few rules for ajax to work with asp.net.

  • Your WebMethod should be
    public
    and
    static
    .
  • If your WebMethod expects some parameter(s) than these parameter(s) must
    be passed as
    data
    in ajax.
  • Name of parameter(s) should be
    same
    in
    WebMethod
    and in
    data
    part
    of ajax.
  • Data passed from ajax should be in
    json string
    .For this you can use
    JSON.stringify
    or you will have to surround the
    values
    of parameter(s)
    in
    quotes
    .

Please check the below sample ajax call

function CallAjax()    {        $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/CallAjax", data: JSON.stringify({ name: "Mairaj", value: "12" }), dataType: "json", async: false, success: function (data) {     //your pre }, error: function (err) {     alert(err.responseText); }        });    }[WebMethod]public static List<string> CallAjax(string name,int value){    List<string> list = new List<string>();    try    {        list.Add("Mairaj");        list.Add("Ahmad");        list.Add("Minhas");    }    catch (Exception ex)    {    }    return list;}

EDIT

If you use

GET
in ajax than you need to enable your webmethod to be called
from
GET
request. Add
[System.Web.script.Services.scriptMethod(UseHttpGet =true)]
on top of WebMetod

[System.Web.Services.WebMethod][System.Web.script.Services.scriptMethod(UseHttpGet = true)]public static int ItemCount()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495408.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号