2009-07-11 84 views
1

我正在使用JQuery AJAX函數將數據傳遞到PHP文檔。使用JQuery Ajax函數返回2組數據

目前,AJAX成功函數返回的HTML被添加到html頁面。

我的目標是讓成功函數返回可用作JavaScript變量的第二個/不同的數據。

這怎麼辦?

更新

問題被回答正確。這是一個結果腳本的例子。

PHP文件

<?php 
$some_html = 'foo'; 
$some_value_for_js_variable = 'bar'; 

// create json content 
echo "{"; 
echo "\"some_html_outupt\":\"$some_html\","; 
echo "\"some_value_for_js_varialbe_output\":\"$some_vale_for_js_variable\""; 
echo "}"; 
?> 

JS文件

// Jquery ajax function 
$.ajax({ 
    dataType: 'json', 
    type: "POST", 
    url: "some_file.php", // the location of the above mentioned php script 
    cache: false, 
    success: function(json) { 
     $(el).html(json['some_html_output']); // add html output to page 
     var a = json['some_value_for_js_varialbe_output']; // assign value to js varialbe 
     } 
    } 
}); // ajax 

回答

3

,我會實現這個的方法是在一個JSON字符串在它2個項目的第一部分將持有的HTML返回數據第二部分保存你想要的變量的數據。

{"html":{html}, 
"2nd variable":"{data}"} 

,然後你可以做一個$ .getJSON調用Web服務器一樣

$.getJSON('path','querystring=if_needed',function(data){ 
    var html = data['html']; 
    var 2ndVariable = data['2nd Variable'] 
    //do other things that you want 
}); 

我希望幫助