[英]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代码中):
dataType: "json"
instead of datatype: "json"
. dataType: "json"
而不是datatype: "json"
。 As the result jqGrid will use default value datatype: "xml"
which is wrong. datatype: "xml"
,这是错误的。 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" }
loadonce: true
option if you didn't implemented server side paging of data. loadonce: true
选项。 LoadGrid
without declaring of the variable and you don't call the function inside of $(document).ready(...);
LoadGrid
不声明变量的情况下分配了LoadGrid
,并且没有在$(document).ready(...);
内部调用该函数$(document).ready(...);
. navGrid
. navGrid
添加导航栏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.