一个Ajax响应更新两个div [英] Update two divs with one AJAX response

查看:194
本文介绍了一个Ajax响应更新两个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有, 我使用jQuery / AJAX调用一个文件,基本保存有人喜欢一首歌或不。我试图做类似如下:

All, I'm using jQuery/AJAX to call a file to basically save it someone likes a song or not. I'm trying to do something like the following:

var html = $.ajax({
type: "POST",
url: "save_song.php",
data: "song_id=" + song_id + "&love_like_hate=hate",
async: false
}).responseText;

$("#div_song_id_"+song_id).html(responseText1);
$("#love_it").html(responseText2);

然后在PHP端是这样的:

Then on the PHP side have something like this:

echo "This text would go in response text 1";
echo "This text would go in response text 2";

所以基本上我想有多个回波在save_song.php文件,然后基本上说第一个回波进入第一个分区,第二回声去,需要更新的第二个分区。任何想法如何做到这一点?

So basically I'm trying to have multiple echo's in the save_song.php file and then basically say the first echo goes to the first div and the second echo goes to the second div that needs updated. Any idea how to do this?

推荐答案

您的PHP code可以给回一个JSON字符串:

Your PHP code could give back a JSON string:

<?php
    echo json_encode(array(
        'test1' => 'This text would go in response text 1',
        'test2' => 'This text would go in response text 2'
    ));
?>

然后你可以在jQuery的解析是:

Then you can parse it in jQuery:

$.ajax({
    type: "POST",
    url: "save_song.php",
    data: "song_id=" + song_id + "&love_like_hate=hate",
    dataType: 'json',
    async: false,
    success: function(response) {
        if (response && response.text1 && response.text2) {
            $("#div_song_id_"+song_id).html(response.text1);
            $("#love_it").html(response.text2);
        }
    }
});

这篇关于一个Ajax响应更新两个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆