2

ここでシングルページアプリの例に従おうとしています

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;
    }
4

1 に答える 1

0

最終的に私の答えを見つけました:

dataType: 'json' を取り出して以下に置き換えました。すべてが良いです。

var loadOptions = { type: 'get', cache: false, contentType: "application/json; charset=utf-8" };

于 2012-10-05T19:49:35.803 に答える