简体   繁体   English

数据表-将Ajax数据源(sAjaxSource)与多个表和动态参数一起使用

[英]Datatables - Using Ajax datasource (sAjaxSource) with multiple tables and dynamic argument

I have two datatables that I am trying to populate with data via a GET request to a flask API. 我有两个数据表,试图通过对烧瓶API的GET请求填充数据。 My datasource url is localhost:5000/data but I am unable to get datatables to display the data. 我的数据源URL是localhost:5000 / data,但是我无法获取数据表来显示数据。 When I create a static .txt file, I can get the data to come through. 创建静态.txt文件时,可以获取数据。 I looked at my GET request and it looks like it is being appended with some sort of event id from jQuery (I am pretty new to this...). 我看了看我的GET请求,它看起来像是从jQuery附加了某种事件ID(我对此很陌生...)。 I would eventually like to be able to pass a custom argument to the GET request in order to filter the second table based on which row in the first table is clicked on by the user. 我最终希望能够将自定义参数传递给GET请求,以便根据用户在第一个表中单击的行来过滤第二个表。

I have experimented with both aaData and sAjaxSource and I cannot get either one to work. 我已经对aaData和sAjaxSource进行了试验,但我都无法使用。

My JSON object is this form: 我的JSON对象是这种形式:

{
  "items": [
    {
      "column1": "Foo", 
      "column2": "Bar", 
      "column3": "1.54"
    }, 
    {
      "column1": "Blah", 
      "column2": "Tah", 
      "column3": "1.54"
    }
  ]
}

Table 1 - I am using a static .txt file and this table displays fine 表1-我使用的是静态.txt文件,此表显示正常

$(document).ready(function() {
    $('#table1').dataTable( {
        "bProcessing": true,
        "sAjaxSource": "/thisWorks.txt",
        "sAjaxDataProp": "item",
        "aoColumns": [
        { 
            "mData": "column1" 
        },
        { 
            "mData": "column2" 
        },
        { 
            "mData": "column3" 
        }
        ]
    } );


    $('#example tbody').on('click', 'tr', function () {
        var clickId = $('td', this).eq(0).text();
    } );

Table 2 - Can't get this one to work 表2-无法使用此功能

$('#table2').dataTable( {
    "bProcessing": true,
    "sAjaxSource": "http://localhost:5000/data?column1=1234",
    "sAjaxDataProp": "items",
    "aoColumns": [
    { "mData": "column1" },
    { "mData": "column2" },
    { "mData": "column3" }
    ]
} );

When I look in my chrome console, I see my second Ajax request being interpreted as something like: 当我在chrome控制台中查看时,我看到第二个Ajax请求被解释为:

http://localhost:5000/data?column1=1234&_1412145757890

Eventually, I would like to pass the value of clickId from my first table to the Ajax source in my second table so any guidance there would be appreciated. 最终,我希望将clickId的值从我的第一个表传递到第二个表中的Ajax源,以便对您的指导有所帮助。

Thanks! 谢谢!

https://softwareengineering.stackexchange.com/questions/216605/how-do-web-servers-enforce-the-same-origin-policy https://softwareengineering.stackexchange.com/questions/216605/how-do-web-servers-enforce-the-same-origin-policy

The same origin policy is a wholly client-based restriction, and is primarily engineered to protect users, not services. 相同的原始策略是完全基于客户端的限制,并且主要是为了保护用户而不是服务而设计的。 All or most browsers include a command-line switch or configuration option to to turn it off. 所有或大多数浏览器都包含一个命令行开关或配置选项以将其关闭。 The SOP is like seat belts in a car: they protect the rider in the car, but anyone can freely choose not to use them. SOP就像汽车上的安全带:它们保护汽车上的骑手,但任何人都可以自由选择不使用它们。 Certainly don't expect a person's seat belt to stop them from getting out of their car and attacking you (or accessing your Web service). 当然,不要指望一个人的安全带阻止他们下车并攻击您(或访问您的Web服务)。

Suppose I write a program that accesses your Web service. 假设我编写了一个访问您的Web服务的程序。 It's just a program that sends TCP messages that include HTTP requests. 这只是一个发送包含HTTP请求的TCP消息的程序。 You're asking for a server-side mechanism to distinguish between requests made by my program (which can send anything) and requests made by a browser that has a page loaded from a permitted origin. 您正在请求一种服务器端机制,以区分程序发出的请求(可以发送任何内容)和浏览器发出的请求(从允许的来源加载页面)。 It simply can't be done; 根本无法做到; my program can always send a request identical to one formed by a Web page. 我的程序始终可以发送与网页形成的请求相同的请求。

The same-origin policy was invented because it prevents code from one website from accessing credential-restricted content on another site. 发明了同源策略,因为它阻止一个网站的代码访问另一站点上受凭证限制的内容。 Ajax requests are by default sent with any auth cookies granted by the target site. 默认情况下,Ajax请求与目标站点授予的所有auth cookie一起发送。 For example, suppose I accidentally load http://evil.com/ , which sends a request for http://mail.google.com/ . 例如,假设我不小心加载了http://evil.com/ ,从而发送了对http://mail.google.com/的请求。 If the SOP were not in place, and I was signed into Gmail, the script at evil.com could see my inbox. 如果没有SOP,并且我已登录Gmail,evil.com上的脚本可以看到我的收件箱。 If the site at evil.com wants to load mail.google.com without my cookies, it can just use a proxy server; 如果evil.com上的网站要加载没有我的cookie的mail.google.com,则可以使用代理服务器; the public contents of mail.google.com are not a secret (but the contents of mail.google.com when accessed with my cookies are a secret). mail.google.com的公开内容不是秘密(但使用cookie进行访问时mail.google.com的内容是秘密)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM