私がやりたいことは、json 形式で (サンプルの Northwind データベースから) 製品の数を返す Web サービスがあり、返された行を jqGrid にバインドしたいということです。私はJQGridを試しました - ASP.NET WebMethodを呼び出すことはできませんが、Ajaxではできますが 、Webサービスでは機能しませんでした。
次に、試しに、jqGrid を作成している同じ aspx ページに [Web メソッド] を書き、mtype:"POST" で動作しました。以下は私のコードです。
Web サービスなしで動作するコード
ASPX のコード:
<script language="javascript" type="text/javascript">
$(function () {
jQuery("#jqGridXML").jqGrid({
url: "jqGridXML.aspx/GetProducts",
mtype: "POST",
ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
datatype: "json",
serializeGridData: function (postData) {
return JSON.stringify(postData);
},
jsonReader: {
root: function (obj) { return obj.d;},
page:function (obj) {return 1;},
total: function (obj) {return 1;},
records:function (obj) { return obj.d.length;},
id:"0",
cell:"",
repeatitems:false
},
datatype:"json",
height:250,
colName:['ProductId','ProductName','UnitsInStock'],
colModel:[{name:'ProductId',index:'ProductId',width:100},
{name:'ProductName',width:100},
{name:'UnitsInStock',width:100}],
caption:"Products"
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table id="jqGridXML"></table>
<div id="pagerXML"></div>
</form>
</body>
.cs ファイルのコード
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<Products> GetProducts()
{
Products product = new Products();
return product.GetProducts();
}
これは機能していますが、フォームに Web メソッドを書きたくない場合。アプリケーションに Web 参照を追加し、Web サービスからのデータを使用したいと考えています。jqGridで以下を試しましたが、役に立ちませんでした。
$(function () {
jQuery("#jqGridXML").jqGrid({
url: "http://localhost:49493/jqGrid_HttpHandlers/WebService.asmx/GetProducts",
mtype: "POST",
残りの部分は同じです。
Web サービスのコード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Collections.ObjectModel;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public static List<Products> GetProducts()
{
Products product = new Products();
return product.GetProducts();
}
}
物事を理解するのを手伝ってくれませんか。jqGridの作業を始めたばかりです。
あなたの助けは本当に感謝しています.
編集
はい、あなたは正しいデイブです。以下のステートメントのコメントを外し、静的を削除して、変更なしでプロジェクトを実行しただけで、完全に機能しました。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
ありがとう、でも次の疑問は、ページング、同じコードでそれらのものを並べ替えるなど、jqGridsナビゲーター機能をどのように使用できるかです。私はこれを使用しました、
jsonReader: {
root: function (obj) { return obj.d;},
page:function (obj) {return 1;},
total: function (obj) {return 1;},
records:function (obj) { return obj.d.length;},
id:"0",
しかし、コードのどこでこれを使用する必要があるのか\u200b\u200bわかりません。正しい解決策にリダイレクトしてください。