jQuery Ajax:我可以在成功时存储多个"变量"吗?

Kar*_*rem 2 javascript php ajax jquery

基本上它只是:

 success: function(msg){
alert(msg);
}
Run Code Online (Sandbox Code Playgroud)

什么出来警报.但是,如果我在ajax调用的文件中有一个var,我是否有可能:

$filename = time() . "10";
Run Code Online (Sandbox Code Playgroud)

成功使用?

所以我能做到

 success: function(msg){
alert($filename);
}
Run Code Online (Sandbox Code Playgroud)

(现在它不正确)但我怎么能这样做?

    $.ajax({
           type: "POST",
           url:"functions.php?action=crop",
           data: {x: $('#x').val(),y: $('#y').val(),w: $('#w').val(),h: $('#h').val(),fname:$('#fname').val(),fixed:fixed,size:size},
           success: function(msg){
               if(!fixed)
                    $("#crop_preview").css({overflow:"auto"})
               $("#crop_preview").html($(document.createElement("img")).attr("src", msg.filename)).show();
           $("#crop_preview").after("Here is your Cropped Image :)").show();
           $("#image").slideUp();
           }
         });
Run Code Online (Sandbox Code Playgroud)

和PHP:

    $time = time().".jpg";
    echo '(';
echo json_encode(array(
    'filename'=>$time
));
echo ')';
Run Code Online (Sandbox Code Playgroud)

aul*_*ron 5

使用PHP json_encode将完整对象返回给客户端:

echo '(';
echo json_encode(array(
    'filename'=>'anything',
    'var2'=>'something',
));
echo ')';
Run Code Online (Sandbox Code Playgroud)

并使用jquery getJSON而不是正常get,或者发出post/ json请求:

$.ajax({
    type: "POST",
    dataType: 'json',
    url:"functions.php?action=crop",
    success: function(response){
        alert(response.filename);
        alert(response.var2);
    }
.....
Run Code Online (Sandbox Code Playgroud)