简体   繁体   English

如何处理JSON响应并从中输出html?

[英]How to process a JSON response and output html from it?

Up until now I have used the django template system to perform this kind of action. 到目前为止,我已经使用django模板系统来执行这种操作。 Simply respond to the ajax request with an html template. 只需使用html模板响应ajax请求即可。

Right now I am trying to implement an autocomplete search feature and I want to send back to the client the response in json format. 现在我正在尝试实现自动完成搜索功能,我想以json格式向客户端发送响应。

All good and set up to this point. 一切都好,并设置到这一点。 This is my jQuery part: 这是我的jQuery部分:

$(document).ready(function(){
    $("#id_q").keyup(function(){ //the form text input
        autocomplete(this.value);
    });

    function autocomplete(inputString) {
        if(inputString.length == 0) {
            $('#autocomplete').fadeOut();
        }
        else {
            $.get("/autocomplete/", {q: ""+inputString+""}, function(data) {
                $('#autocomplete').fadeIn();
                $('#autocomplete').html(data);
            });
        }
    }
});

When using a django template as a response, the #autocomplete div was showing up nice and as expected with .html(data), thats because I was setting up the html in the template beforehand, as I wanted it to be shown. 当使用django模板作为响应时,#autocomplete div显示出很好的效果和预期的.html(数据),那是因为我之前在模板中设置了html,因为我想要显示它。

How do I process the data sent from the server (in json format)? 如何处理从服务器发送的数据(以json格式)? Data looks something like this: 数据看起来像这样:

[{'title':'titleString', 'descr':'desriptionString', 'url':'itemAbsoluteUrl'}, ..]

In order to obtain my #autocomplete html, say, something like: 为了获得我的#autocomplete html,比如说:

<li><a href="data.url">data.title<br>data.descr</li>

Thanks for any feedback! 感谢您的任何反馈!

First of all, in order to format the return data as a JSON object, you need to specify in the get call that the return type is json. 首先,为了将返回数据格式化为JSON对象,您需要在get调用中指定返回类型为json。 After that, you reference data based on the json key value pairs, eg data.results. 之后,您将基于json键值对引用数据,例如data.results。 (Note that results must be a key in your JSON object). (请注意,结果必须是JSON对象中的键)。

$.get("/autocomplete/", {q: ""+inputString+""}, function(data) {
            $('#autocomplete').fadeIn();
            $('#autocomplete').html(data.results);
}, "json");

I'm not sure what your current format is, but just to clarify, you probably don't want to send raw HTML through JSON. 我不确定您当前的格式是什么,但只是为了澄清,您可能不希望通过JSON发送原始HTML。 Instead, keep the HTML on your client page, and have get return some data to populate it. 相反,将HTML保留在客户端页面上,并返回一些数据来填充它。

Try 尝试

var li = $('<li><a href="'+data.url+'">'+data.title+'<br/>'+data.descr+'</a></li>')

and then you can insert li where you want, or 然后你可以在你想要的地方插入li ,或者

$('#autocomplete').html('<li><a href="'+data.url+'">'+data.title+'<br/>'+data.descr+'</a></li>');

To iterate through the results 迭代结果

$.each(results, function(index, value){
...
})

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

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