【问题标题】:Return a JS to ajax var将 JS 返回到 ajax var
【发布时间】:2016-06-20 14:29:01
【问题描述】:

所以我正在做的是我用 get resquest 加载一些数据,我在 httpRequest.responseText 上得到这个变量,我使用 datatables 有一些花哨的表,但问题是我无法在 ajax:field 上加载我的 js var。 .

 <script type="text/javascript">
        var httpRequest = null;

        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = '/resources/ud_auto_executions';
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange() {
            if (httpRequest.readyState==4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                alert ("hello im coming");

                }
                else {
                    alert ("Operation failed.");
                }
            }
       }
</script>

我在 ajax 上有问题:

  <script>
        $(document).ready(function() {
        $('#example').DataTable( {
            "AutoWidth": true,
            "ajax": httpRequest.responseText

【问题讨论】:

  • 这不是同一个问题
  • @MedOulaidi 您必须解释为什么您认为链接的答案不适用于此处。您正尝试在文档准备就绪 before 中使用httpRequest.responseText,它已从异步调用返回 - 即您需要有关如何返回异步调用响应的帮助 - 即链接问题。也许您只需要查找“异步”?很难说。
  • @freedomn-m 我只是不知道如何在我的情况下应用它......
  • 只需将您的 $().DataTable 调用移动到 ajax 响应 (OnStateChange) 回调中,然后 httpRequest.responseText 将在范围内。 if (IsRequestSuccessful (httpRequest)) { 变为 if (IsRequestSuccessful (httpRequest)) { $("#example").DataTable({...

标签: javascript jquery json ajax datatables


【解决方案1】:

你把事情复杂化了。试试看:https://datatables.net/examples/index

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多