我正在使用JQuery AJAX函数将数据传递到PHP文档。
当前,AJAX成功函数返回HTML,该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
最佳答案
我实现此方法的方式是返回JSON字符串中包含2个项目的数据。第一部分将保存HTML,第二部分将保存所需变量的数据。
{"html":{html},
"2nd variable":"{data}"}
然后您可以像这样对Web服务器进行$ .getJSON调用
$.getJSON('path','querystring=if_needed',function(data){
var html = data['html'];
var 2ndVariable = data['2nd Variable']
//do other things that you want
});
希望对您有所帮助
关于javascript - 使用JQuery Ajax函数返回2组数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1113807/