如何将 jquery 响应与 $.post 分开


How to separate jquery response from $.post?

jquery $.post:

$.post(
    'includes/studiesAjax/addTryb.php',
    {inputVL: inputVL},
    function(res){
        $("#container").html(res);
    }
);

响应是长 HTML 代码

我想从响应的第一行中提取数据 beetween <p> 标签,将其与响应分开并分配给新变量。怎么办?

function(res){
    var newVar = $(res).find('p:first').html();
}

您应该将响应转换为 jquery 对象,然后您可以像往常一样进行操作:

$.post(
            'includes/studiesAjax/addTryb.php',
            {inputVL: inputVL},
            function(res){
                var result = $(res).find('p:first').html();
                $("#container").html(result );
            }
        );

如果你返回json而不是html,你可以这样做。

       $.post(
            'includes/studiesAjax/addTryb.php',
            {inputVL: inputVL},
            function(res){
                var data = JSON.parse(res);
                if(data && data.response1){
                    result = (data.response1);
                    console.log(result);
                }
                if(data && data.response2)
                    $("#container").html(data.response2);
            }
        );