Sha*_*pta 8 jquery json asmx jqgrid
我一直在寻找过去3个小时的100个链接,例如将scriptfactory添加到webconfig,3个错误,设置内容类型等.
我无法弄清楚究竟是什么错误.
环境:在.net 4.0上运行的.net 4.0 Web应用程序上运行的服务
要求:我需要将jqGrid绑定到asmx Web服务,该服务将json作为字符串返回给我. Web服务文件包含以下代码.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SampleService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetJsonServerProcess()
{
int memory = 1;
string json = string.Empty;
var obj = (System.Diagnostics.Process.GetProcesses().Where(r => r.WorkingSet64 > memory).Select(p => new { p.ProcessName, p.WorkingSet64 }).ToArray());
json = Lib.ToJSON(obj);
return json;
}
}
Run Code Online (Sandbox Code Playgroud)
Javascript如下
<script type="text/javascript">
$(document).ready(function () {
jQuery("#jqgajax").jqGrid({
ajaxGridOptions: { type: "POST", contentType: 'application/json; charset=utf-8' },
url:'http://localhost:1092/SampleService.asmx/GetJsonServerProcess',
datatype: "json",
data: "{}",
colNames: ['ProcessName', 'WorkingSet64'],
colModel: [
{ name: 'ProcessName', index: 'ProcessName', width: 55 },
{ name: 'WorkingSet64', index: 'WorkingSet64', width: 90 }
],
rowNum: 10,
width: 700,
rowList: [10, 20, 30],
sortname: 'invdate',
viewrecords: true,
sortorder: "desc",
caption: "New API Example"
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
HTML如下
<table id="jqgajax">
</table>
<div id="jqgajax">
</div>
Run Code Online (Sandbox Code Playgroud)
单击"调用"按钮时的Web服务输出
<string xmlns="http://tempuri.org/">
[{"ProcessName":"Dropbox","WorkingSet64":22736896},
{"ProcessName":"fdhost","WorkingSet64":1941504},
{"ProcessName":"IntelliTrace","WorkingSet64":39276544}
]
</string>
Run Code Online (Sandbox Code Playgroud)
请建议我缺少什么.
<string xmlns="http://tempuri.org/">标签让我很烦.我假设这些标签不让我的网格能够绑定.
更新:
ASMX服务现在如下所示.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SampleService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<demo> GetJsonServerProcess()
{
List<demo> test = new List<demo>();
for(int i=1;i<=10;i++)
test.Add(new demo { ProcessName = string.Format("Sample {0}",i), WorkingSet64 = i });
var re = test;
return re;
}
}
public class demo
{
public string ProcessName { get; set; }
public int WorkingSet64 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
单击Invoke按钮返回XML,因为请求未指定contentType: 'application/json; charset=utf-8'.因此,单击"调用"按钮的实验并不能帮助您.
代码中的主要问题是您将数据转换为Web方法内的字符串.这条线
json = Lib.ToJSON(obj);
Run Code Online (Sandbox Code Playgroud)
不需要.通常做的是返回对象.该GetJsonServerProcess应改为类似
[ScriptService]
public class SampleService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Process> GetJsonServerProcess()
{
int memory = 1;
return System.Diagnostics.Process.GetProcesses()
.Where(r => r.WorkingSet64 > memory)
.Select(p => new { p.ProcessName, p.WorkingSet64 })
.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
接下来的问题是等待jqGrid的默认输入格式是另一个(见这里).所以你要指出jsonReader哪些描述数据格式.在你的情况下,它会是这样的
jsonReader: {
repeatitems: false,
id: "ProcessName",
root: function (obj) { return obj; },
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) { return obj.length; }
}
Run Code Online (Sandbox Code Playgroud)
此外,您永远不应http://localhost:1092/在Ajax中使用前缀,url因为出于安全原因,您只能从同一站点获取数据.datajqGrid中的参数具有jQuery中的另一个含义,因此您应该删除data: "{}"并type: "POST"从中移动ajaxGridOptions到mtype: "POST".结果你将有类似的东西
$(document).ready(function () {
$("#jqgajax").jqGrid({
mtype: "POST",
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
url: '/SampleService.asmx/GetJsonServerProcess',
postData: "{}", // remove all parameters which jqGrid send typically
datatype: "json",
colNames: ['ProcessName', 'WorkingSet64'],
colModel: [
{ name: 'ProcessName', index: 'ProcessName', width: 155 },
{ name: 'WorkingSet64', index: 'WorkingSet64', width: 190 }
],
jsonReader: {
repeatitems: false,
id: "ProcessName",
root: function (obj) { return obj; },
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) { return obj.length; }
},
rowNum: 10,
loadonce: true,
gridview: true,
height: 'auto',
rowList: [10, 20, 30],
viewrecords: true,
sortorder: "desc",
caption: "New API Example"
});
});
Run Code Online (Sandbox Code Playgroud)
我没有测试代码,但它应该更接近你需要的.
更新:您应该通过更改来修复代码jsonReader.您可以在此处下载工作演示.它显示网格

我在服务器端使用了代码
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web.Services;
namespace jqGridWebASMX
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class SampleService : WebService
{
[WebMethod]
public List<Demo> GetJsonServerProcess()
{
const int memory = 1;
return Process.GetProcesses()
.Where (r => r.WorkingSet64 > memory)
.Select(p => new Demo {
Id = p.Id,
ProcessName = p.ProcessName,
WorkingSet64 = p.WorkingSet64
})
.ToList();
}
}
public class Demo
{
public int Id { get; set; }
public string ProcessName { get; set; }
public long WorkingSet64 { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
并在客户端
$("#list").jqGrid({
mtype: "POST",
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
url: '/SampleService.asmx/GetJsonServerProcess',
postData: "{}", // remove all parameters which jqGrid send typically
datatype: "json",
colNames: ['ProcessName', 'WorkingSet64'],
colModel: [
{ name: 'ProcessName', index: 'ProcessName', width: 200 },
{ name: 'WorkingSet64', index: 'WorkingSet64', width: 120,
formatter: 'integer', sorttype: 'int', align: 'right' }
],
jsonReader: {
repeatitems: false,
id: "Id",
root: function (obj) { return obj.d; },
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) { return obj.d.length; }
},
rowNum: 10,
loadonce: true,
gridview: true,
height: 'auto',
pager: '#pager',
rowList: [10, 20, 30],
rownumbers: true,
viewrecords: true,
sortorder: "desc",
caption: "New API Example"
});
$("#pager_left").hide(); // hide unused part of the pager to have more space
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25429 次 |
| 最近记录: |