jsrender模板不会渲染,任何想法?

时间:2014-04-28 23:23:17

标签: javascript jquery jsrender

有谁能告诉我为什么这个模板不会渲染?

我正在渲染它:

 var v = jQuery.url().param("id");
        jQuery.ajax({
            post: "POST",
            url: "svc.asmx/FetchNewsDetail",
            dataType: "text",
            contentType: "application/json; charset=utf-8",
            data: { 'id': v },
            success: function (data) {

                jQuery("#blog").append(jQuery("#newsSingleTemplate").render(data));
            }
        });

这是我的json字符串,我通过web方法(asp.net,asmx)收到:

{"newsDetailId":1,"newsId":1,"newsItemTitle":"Make Your Reservations Before It\u0027s Too Late","newsItemDate":"\/Date(1400652000000)\/","newsItemBannerUrl":"img/news/bass_single_news.jpg","newsItemBannerAlt":"BASS Divisional","newsItemText":"Join us at the Noxon Reservoir in Trout Creek, Mont., for the 2014 B.A.S.S. Nation Western Divisional. This fishery is in the northwest corner of Montana, very close to Idaho and only a 2-hour drive to Canada. Montana has hosted B.A.S.S. Nation divisionals, but it has not hosted other levels of B.A.S.S. tournaments. Competitors will vie for the Western title May 21-23.","newsItemQuote":"I think what I like most about B.A.S.S. Nation divisionals is the opportunity to go to places that you don\u0027t often think of as well-known tournament destinations, such as Noxon in Montana and Lake Monroe in Indiana\"- Jon Stewart, director of the B.A.S.S. Nation"}{"d":null}

这是我的模板

            <script id="newsSingleTemplate" type="text/x-jsrender">
            <article>
                <section class="first-column">
                    <header>
                        <h2>{{:newsItemTitle}}</h2>
                    </header>
                    <div class="date">
                        {{:newsItemDate}}
                    </div>
                    <p>
                        <img src="{{url:newsItemBannerUrl}}" width="670" height="300" alt="{{:newsItemBannerAlt}}" />
                        <br />{{:newsItemText}}</p>
                    <div id="slogan">
                        <h1>{{:newsItemQuote}}</h1>
                    </div>
                </section>
            </article>
            </script>

感谢你的帮助!

2 个答案:

答案 0 :(得分:1)

使用a fiddle成功测试。你的问题没有明确表达。究竟什么不起作用?您的模板在哪种上下文中?你使用什么数据结构?

var template = $.templates("#newsSingleTemplate");

template.link("#result", data);

答案 1 :(得分:0)

它与我的json字符串末尾的{&#34; d&#34;:null}有关。

我的ajax电话不正确。以下是解决渲染问题的方法。我不明白为什么数据参数是正确的。

var v = jQuery.url().param("id");
        jQuery.ajax({
            post: "POST",
            url: "svc.asmx/FetchNewsDetail",
            data: "id=" + v,
            success: function (data) {
                jQuery("#blog").html(jQuery("#newsSingleTemplate").render(data));
            }
        });