ここでシングルページアプリの例に従おうとしています
JSON が解析されるはずの場所でエラーが発生します: SyntaxError: JSON.parse: unexpected character if I leave dataType: 'json' If I put in dataTyp: 'json', then no errors, but json is not returned and私のテンプレートは記入されていません。
グリッドビューを入れて、リストが返されたかどうかを確認します...そしてそうです。
jquery、sammy.js、json2.js、および sammy.template.js をロードしました。すべて最新かつ最高です。
私はそれが diff サイトのサンプル アプリであることを知っています。皆さんの時間に感謝します。SPAアプリはエキサイティングなので、フォローして理解するのが好きです.
JavaScript:
$(document).ready(function () {
//alert('Jquery has loaded');
var loadOptions = { type: 'get', dataType: 'json', cache: false };
var app = $.sammy('#sammyDiv', function () {
this.use('Template');
this.use('Session');
this.around(function (callback) {
var context = this;
this.load('Default.aspx/GetEmployees', { cache: false }) // loadOptions ) // { dataType: 'json', cache: false })
.then(function (items) {
context.items = JSON.parse(items).d; // browser parser... I believe it fails right here
//context.items = $.parseJSON(items).d; //jQuery handling
//alert(context.items);
//context.items = JSON.stringify(items);
})
.then(callback);
});
csコード:
static List<Employees> emps;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
emps = new List<Employees>();
Employees emp = new Employees("Kedar", "Kulkarni", "0001", "A");
Employees emp1 = new Employees("ABC", "XYZ", "21211", "B");
emps.Add(emp);
emps.Add(emp1);
}
/* using foreach method
foreach (Person person in persons)
{
Response.Write(String.Format("Name : {0} Age : {1} Address : {2} Job : {3}", person.Name, person.Age, person.Address, person.Job));
Response.Write("</br>");
}
*/
this.GridView1.DataSource = emps; //for testing to see if my list gets returned
this.GridView1.DataBind();
}
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public static List<Employees> GetEmployees()
{
return emps;
}