2010-09-02 36 views
2

基本上它只是:jQuery Ajax:我能成功存储多个“变量”吗?

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

什么出来alert's。但对我来说,如果我在ajax调用的文件中有变量:

$filename = time() . "10"; 

要成功使用吗?

,所以我可以做

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

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

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

和PHP:

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

回答

5

使用PHP json_encode,满对象返回给客户端:

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

和使用jQuery的getJSON而不是正常get,或作出post/json请求:

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

即时通讯使用ajax所以我该如何处理postJSON?请检查更新的问题 – Karem 2010-09-02 18:47:22

+0

正如你可以看到在上面的脚本我试图做msg.filename但没有结果 – Karem 2010-09-02 18:50:21

+0

我编辑我的答案,更新和检查。 – aularon 2010-09-02 18:56:08