jQuery的阿贾克斯:我可以存储多个"变"成功? [英] jQuery Ajax: Can i store more than one "variable" on success?

查看:128
本文介绍了jQuery的阿贾克斯:我可以存储多个"变"成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 成功:函数(MSG){
警报(味精);
}
 

什么出来alert's。但是,它可能对我来说,如果我有一个文件的VAR的Ajax调用:

  $文件名=时间()。 10;
 

在成功使用?

所以我可以做

 成功:函数(MSG){
警报($文件名);
}
 

(现在它不正确的),但我怎么能做到这一点?

  $。阿贾克斯({
           键入:POST,
           网址:functions.php的行动=作物,
           数据:{X:$('#X')VAL(),Y:$('#Y')VAL(),W:$('#W')VAL(),H:$(' #时)。VAL(),FNAME:$('#FNAME)VAL(),固定:固定的,大小:大小},
           成功:函数(MSG){
               如果(!固定)
                    $(#crop_ preVIEW)的CSS({溢出:自动})
               $(#crop_ preVIEW)HTML($(document.createElement方法(IMG))ATTR(src用户,msg.filename)。)显示()。
           $(#crop_ preVIEW)后(这是你的裁切图像:))显示()。
           $(#图像)效果基本show()。
           }
         });
 

和PHP的:

  $时间=时间()。JPG;
    回声 '(';
回声json_en code(阵列(
    '文件名'=> $时间
));
回声 ')';
 

解决方案

使用PHP json_en code,回满对象的客户端:

 回声(;
回声json_en code(阵列(
    '文件名'=>'什么',
    VAR2=>'东西',
));
回声 ')';
 

和使用jQuery的的getJSON 而不是普通的 GET ,或作出发表 / JSON 要求:

  $。阿贾克斯({
    键入:POST,
    数据类型:JSON,
    网址:functions.php的行动=作物,
    成功:函数(响应){
        警报(response.filename);
        警报(response.var2);
    }
.....
 

 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:

$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:

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

解决方案

Use PHP json_encode, to return full objects to client:

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:

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

这篇关于jQuery的阿贾克斯:我可以存储多个"变"成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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