简体   繁体   English

无法在JavaScript中调用C#方法

[英]Unable to call C# method in javascript

i want to call my webmethod( C# method) in aspx page in javascript ajax call. 我想在javascript ajax调用的aspx页面中调用我的webmethod(C#方法)。 so in URL i can pass the Test.aspx/MyMethodName 所以在URL中我可以通过Test.aspx / MyMethodName

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string Loadsites() {
    var jsondata = JsonConvert.SerializeObject(lstsitename);
    // test.Visible = true; return jsondata;
} 
$(document).ready(function () {        
   LoadGrid = function () {
    alert('Loading Grid data');
    jQuery("#Grid").jqGrid({           
        mtype: 'POST',
        url: 'Test.aspx/Loadsites',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        colNames: ['SiteName', 'Description' ],
        colModel: [
                    { name: 'SiteName', index: 'SiteName' },
                    { name: 'Description', index: 'Description' }
        ],
        sortname: "SiteName",
        rowNum: 10,
        viewrecords: true,
        sortorder: "asc",
        caption: "Site Details",
        width: "auto",
        height: "auto",
        pager: '#Pager'

    });

    jQuery("#Grid").jqGrid('navGrid', '#Pager', { edit: true, add: true, del: true });
 }
});

i tried static in method but didnt work as well 我在方法中尝试了静态方法,但也无法正常工作

If you place this attribute [WebMethod] above your method signature, you could achieve that you want. 如果将此属性[WebMethod]放置在方法签名上方,则可以实现所需的功能。

Furthermore, you method should be defined as static . 此外,您的方法应定义为static

There are errors on both sides (on server side and on the client side). 双方都有错误(服务器端和客户端)。

On the server side you declare string as the result of Loadsites and you call JsonConvert.SerializeObject explicitly. 在服务器端,您将string声明为Loadsites的结果,并显式调用JsonConvert.SerializeObject It's wrong. 这是不对的。 WebMethod is designed so that it makes conversion to JSON or XML internally . WebMethod的设计使其可以在内部转换为JSON或XML。 So one should return object (just return lstsitename ;) instead of string . 因此,应该返回object (仅return lstsitename ;)而不是string WebMethod will converts the results to JSON based on contentType of the HTTP request. WebMethod将根据HTTP请求的contentType将结果转换为JSON。 If you call JsonConvert.SerializeObject additionally then the returned data is string which contains quotes, for example [{"myCol1": "value1"}] . 如果另外调用JsonConvert.SerializeObject则返回的数据为包含引号的字符串,例如[{"myCol1": "value1"}] Because of JSON standards such string will be converted to JSON once more and the resulting data returned from the server will be like "[{\\"myCol1\\": "value1\\"}]" . 由于JSON标准,此类字符串将再次转换为JSON 并且从服务器返回的结果数据将类似于"[{\\"myCol1\\": "value1\\"}]" Conversion of correct JSON data [{"myCol1": "value1"}] would get array of objects in JavaSript on the client side. 正确的JSON数据[{"myCol1": "value1"}]将在客户端的JavaSript中获得对象数组 Parsing the twice converted data "[{\\"myCol1\\": "value1\\"}]" will produce the string '[{"myCol1": "value1"}]' instead of array of objects. 解析两次转换的数据"[{\\"myCol1\\": "value1\\"}]"会产生字符串 '[{"myCol1": "value1"}]'而不是对象数组。

There are some important errors on the client side too (in the JavaScript code which creates jqGrid): 客户端也有一些重要的错误(在创建jqGrid的JavaScript代码中):

  • you use unknown option dataType: "json" instead of datatype: "json" . 您使用未知选项dataType: "json"而不是datatype: "json" As the result jqGrid will use default value datatype: "xml" which is wrong. 结果jqGrid将使用默认值datatype: "xml" ,这是错误的。
  • you use unknown option contentType: "application/json; charset=utf-8" instead of the usage of ajaxGridOptions which allows to specify Ajax options: ajaxGridOptions: { contentType: "application/json; charset=utf-8" } 您使用未知的选项contentType: "application/json; charset=utf-8"代替的使用ajaxGridOptions这允许指定的Ajax选项: ajaxGridOptions: { contentType: "application/json; charset=utf-8" }
  • you should add loadonce: true option if you didn't implemented server side paging of data. 如果未实现服务器端数据分页,则应添加loadonce: true选项。
  • you assign LoadGrid without declaring of the variable and you don't call the function inside of $(document).ready(...); LoadGrid不声明变量的情况下分配了LoadGrid ,并且没有在$(document).ready(...);内部调用该函数$(document).ready(...); . Instead of that you should just execute once the corresponding code which creates jqGrid and add navigator bar by calling of navGrid . 取而代之的是,您只需执行一次相应的代码即可创建jqGrid并通过调用navGrid添加导航栏。

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

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