Using jQuery ajax response data

前端 未结 3 1324
眼角桃花
眼角桃花 2021-02-20 17:13

I am using ajax post and am receiving data in the form of html. I need to split up the data and place pieces of the data all over the page. I built my response data to be someth

3条回答
  •  故里飘歌
    2021-02-20 17:35

    Update: Just realized you should probably do this:

    success:function(data) {
        data = $('
    ').append(data); $('#greeting',data).appendTo('#one') $('#something',data).appendTo('#two') }

    As you cant use .find properly since it isnt a child but if you append it to an empty node you can. The other alternative would be using .filter

    $.ajax({
                type:'POST',
                url: 'confirm.php',
                data: "really=yes&sure=yes",
                success:function(data){
                        $('#greeting',data).appendTo('#one')
                        $('#something',data).appendTo('#two')
                }
            });
    

    You can extract from data and append where you want to. You can also do something like return JSON instead, and instead of extracting html from html just access the html from an object.

    $(data.greeting).appendTo('#one')
    $(data.something).appendTo('#two')
    

    The response would have to be like:

    ({ 'greeting':'html', 'something' :'other html' })
    

提交回复
热议问题