简体   繁体   English

将html div传递给ajax调用

[英]Pass html div to ajax call

So I have my server-side code which executes multiple bigquery queries and arranges the results in a table,and this server-side code is called by an ajax call from the client. 所以我有我的服务器端代码执行多个bigquery查询并将结果排列在一个表中,这个服务器端代码由来自客户端的ajax调用调用。 I want to be able to send the table/div from my server-side code to client-side and have it render there. 我希望能够将表/ div从我的服务器端代码发送到客户端并在那里进行渲染。 Is that possible?? 那可能吗??

I don't want to be able to create the tables on the client by first getting the JSON results through the call, since I will not know which query will run first and all the results are different. 我不希望能够通过首先通过调用获取JSON结果来在客户端上创建表,因为我不知道哪个查询将首先运行并且所有结果都不同。 (multiple ajax calls are also out of the question for each query) (对于每个查询,多个ajax调用也是不可能的)

Server code (app.js) 服务器代码(app.js)

 function printResult(rows, queryNumber) {
    console.log('Query No. '+queryNumber+' Results:____');
    var keys = [];
    if (queryNumber == 1) {
        var table = document.createElement('table');

        var td = [];
        var tr = document.createElement('tr');
        for (var i = 0; i < rows[0].length; i++) {
            var tn1 = document.createTextNode(rows[i].Content_title);
            var tn2 = document.createTextNode(rows[i].Audience_Size);

            td[i] = document.createElement('td');
            td[i].appendChild(tn1);
            td[i + 1] = document.createElement('td').appendChild(tn2);

            tr.appendChild(td[i]);
            tr.appendChild(td[i + 1]);
            table.appendChild(tr);
            res.send(table);
        }
    }
}

Ajax call Ajax调用

$.ajax({
    url: 'http://localhost:3000/example',
    type: 'POST',
    data: {showname: show, counter: uniquesOverallShowsCounter},
    dataType: 'text',
    contentType: 'application/x-www-form-urlencoded',
    success: function (data) {




        document.getElementById('outputTables').appendChild(data);


    },
    error: function() {
        console.log("error");
    }
});

Yes you can, but as you pointed out in the comments, there is a problem with the data returned. 是的,你可以,但正如你在评论中指出的那样,返回的数据存在问题。

Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. 无法在'Node'上执行'appendChild':参数1不是'Node'类型。

This is an error which is thrown when you try to append a child node with JavaScript, which is not a documentElement (ie document.createElement('div') ). 当您尝试使用JavaScript追加子节点时,抛出此错误,该JavaScript不是documentElement(即document.createElement('div') )。 Since you're already using jQuery, you could easily change your code. 由于您已经在使用jQuery,因此可以轻松更改代码。

In the success function for the AJAX call, change 在AJAX调用的成功函数中,进行更改

document.getElementById('outputTables').appendChild(data);

to

$('#outputTables').append(data);

If you want to be able to do multiple AJAX calls from one page and change the table every time (like replace the old table with the new one), you should use html(data) instead of append(data) . 如果您希望能够从一个页面执行多个AJAX调用并且每次都更改表(比如将旧表替换为新表),则应使用html(data)而不是append(data)

So can you simply increase one more parameter in your response json based on the query which is hit for example "queryProcessed"? 那么你可以简单地根据被查询的查询(例如“queryProcessed”)在响应json中再增加一个参数吗?

And add that additional check on client side and render the mark up based on that "queryProcessed" 并在客户端添加额外检查并基于“queryProcessed”呈现标记

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

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