使用AJAX将PHP变量发送到javascript [英] send Php variables to javascript using AJAX
本文介绍了使用AJAX将PHP变量发送到javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个PHP变量,我想使用AJAX传递给javascript.
I have two PHP variables that I want to pass to javascript using AJAX.
test.php
$Longitude = json_encode($Long);
$Latitude = json_encode($Lat);
Index.js
$.ajax({
type: 'POST',
dataType: "json",
url:'test.php',
data:
success: function(data)
{
console.log("testing");
}
});
我是编程新手.请指导如何在ajax调用中引用这些变量名称.
I am new to programming. Please guide how to refer these variable names in the ajax call.
推荐答案
传入数组并对其进行编码.
Pass in array and encode it.
test.php
$data = ['Longitude ' =>$Long, 'Latitude ' => $Lat ];
echo json_encode($data);
index.js
$.ajax({
type: 'POST',
dataType: "json",
url:'test.php',
data:
success: function(data)
{
try {
data = JSON.parse(data);
}catch(e) {}
console.log(data);
}
});
这篇关于使用AJAX将PHP变量发送到javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文