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

时间:2022-04-28 15:42:20

Basically it's just:

基本上它只是:

 success: function(msg){
alert(msg);
}

What comes out alert´s. But is it possible for me if i have a var in the file that the ajax call:

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

$filename = time() . "10";

to use in success?

成功使用?

So i could do

所以我能做到

 success: function(msg){
alert($filename);
}

(now its not correct) but how can i do this?

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

    $.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();
           }
         });

And php:

和PHP:

    $time = time().".jpg";
    echo '(';
echo json_encode(array(
    'filename'=>$time
));
echo ')';

1 个解决方案

#1


5  

Use PHP json_encode, to return full objects to client:

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

echo '(';
echo json_encode(array(
    'filename'=>'anything',
    'var2'=>'something',
));
echo ')';

and use jquery's getJSON instead of normal get, or make a post/json request:

并使用jquery的getJSON而不是普通的get,或者发一个post / json请求:

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

#1


5  

Use PHP json_encode, to return full objects to client:

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

echo '(';
echo json_encode(array(
    'filename'=>'anything',
    'var2'=>'something',
));
echo ')';

and use jquery's getJSON instead of normal get, or make a post/json request:

并使用jquery的getJSON而不是普通的get,或者发一个post / json请求:

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