te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Embed raw data in HTML to parse in jQuery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Embed raw data in HTML to parse in jQuery - Stack Overflow

programmeradmin3浏览0评论

I've been living in the desktop world for most of my career, so forgive me for asking such a basic question, but I'm not quite sure where to start looking.

I want to return some raw data along with my HTML, and parse and display the data using jQuery as soon as the HTML is ready. I know roughly what my js code should look like, but I'm not sure how I should embed the raw data in my HTML.

I could use $.getJSON(), but it'd be better if I could have the data right in my HTML.

I think either json or XML would work, but what's the proper way to escape/embed/parse these when they're embedded in HTML?

Thanks in advance.

I've been living in the desktop world for most of my career, so forgive me for asking such a basic question, but I'm not quite sure where to start looking.

I want to return some raw data along with my HTML, and parse and display the data using jQuery as soon as the HTML is ready. I know roughly what my js code should look like, but I'm not sure how I should embed the raw data in my HTML.

I could use $.getJSON(), but it'd be better if I could have the data right in my HTML.

I think either json or XML would work, but what's the proper way to escape/embed/parse these when they're embedded in HTML?

Thanks in advance.

Share Improve this question asked Aug 8, 2010 at 17:51 Rei MiyasakaRei Miyasaka 7,1266 gold badges43 silver badges71 bronze badges 2
  • What kind of data would that be, and what would the end result look like? Can you make an example? – Pekka Commented Aug 8, 2010 at 17:59
  • The data is a list of labels, names and values for form input fields. I want to generate the tags dynamically, because otherwise I have to do the HTML twice -- once for the initial content, and again embedded in js strings to emit using jQuery when I add more fields. – Rei Miyasaka Commented Aug 8, 2010 at 18:03
Add a ment  | 

3 Answers 3

Reset to default 7

You can put the JSON data in a hidden div, then decode and use from jQuery.

For example, we'll take:

{"foo":"apple","bar":"orange"}

Escape it and put in a div:

<div id="data">%7B%22foo%22%3A%22apple%22%2C%22bar%22%3A%22orange%22%7D</div>

Then from jQuery, we can use the data:

var data = jQuery.parseJSON(unescape($("#data").html()));

So calling alert(data.foo) will give us apple.

Here's a working example.

Where and when do you want this data?

If you want it in your view, just pass the data to the view

Action/Controller:

public ActionResult MyAction()
{
    ViewData["MyData"] = "this is a sample data of type string";
    return View();
}

And then, somewhere in your view:

<script>
    var data = '<%= ViewData["MyData"] %>';
    $(document).ready(){
        alert(data);
    }
</script>
<h1><%: ViewData["MyData"] %></h1>

Of course, if you're working with a List<string> or `string[]', you would need to format it to proper JavaScript for jQuery to understand it.

<script>
     var dataArray = [

     <% foreach(string s in (string[])ViewData["MyDataArray"]){ %>
        <%= s %>,
     <% } %>   
     ];
</script>

It would be getter if you generated the proper JavaScript in the action instead of the view to avoid ugly markup in your view:

public ActionResult MyAction()
{
    string[] myArray = new string[]{ "hello", "wolrd" };
    ViewData["MyData"] = myArray;
    ViewData["JavaScriptArray"] = "[" + myArray.Aggregate((current,next) => string.Format("'{0}','{1}',", current, next).TrimEnd(new char[] { ','})) + "]";
    // or you can use your favorite JavaScript serialize
    return View();
}

Now you can do the following in your view:

<script>
    var dataArray = <%= ViewData["MyJavaScriptArray"] %>;
    alert(dataArray[0]); // alerts 'hello'
</script>

Like you said it is probably best to get it via Ajax using $.post or $.get or $(element).load() etc...

But if you must save it in the page it is mon to save in a hidden field. Asp saves things in hidden fields using binary serialization and Base64 but you can save it as a Json string and then use it in your JS.

发布评论

评论列表(0)

  1. 暂无评论