2012-10-05 150 views
2

我試圖按照單頁應用例子here返回JSON數據和sammy.js

身在何方JSON應該被解析出現錯誤: 語法錯誤:JSON.parse:意外的字符如果我省略數據類型:'json' 如果我把dataTyp:'json',然後沒有錯誤,但json不返回,我的模板沒有填充。

我把一個gridview只是看看我的列表正在返回...並且確實如此。

我已經加載,jquery,sammy.js,json2.js和sammy.template.js..all最新最好。

我知道它是差異網站上的示例應用程序,我很欣賞你們的時間。我只是喜歡跟隨和理解,因爲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; 
    } 

回答

0

終於找到了我的答案:

我拿出數據類型: 'JSON' 和下面所取代。一切都很好。我們可以通過下面的例子來說明這個問題: