我有这个JS:
<script>
jQuery(document).ready(function(){ // we wait until DOM is ready
jQuery('#veranstort').change(function(){ // fire when input is filled
origin = "55767 Schwollen";
destination = "13509 Berlin";
jQuery.ajax({ // we build the AJAX request
method:"POST",
url:"index.php?option=com_rsform&formId=6&action=ajax",
data: {origin, destination},
success: function(results) {
console.log("results: " + results);
}
});
});
})
</script>
触发此php脚本:
$action = JRequest::getWord('action'); // we will need a parameter to run the script
if ($action == "ajax") { // if condition is met, run the script
$origin = $_POST['origin']; // get the origin
$destination = $_POST['destination']; // get the destination
var_dump("destination: ".$destination); // this gives me NULL!!
$distance = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$origin."&destinations=".$destination."&key=GMAPSKEY"); // build the URL according to the API
$distance = json_decode($distance); // decode the response from JSON
print_r($distance->rows[0]->elements[0]->distance->text);die(); // print the result so we can catch it in the AJAX call
}
这已经有一段时间了,但是现在没有了。我无法在php中访问目标或起点值。我在做什么错了?
答案 0 :(得分:0)
错误是由于网址中缺少/
而引起的。它被重定向,并且POST数据在此过程中丢失了。
正确的网址:
url:"/index.php?option=com_rsform&formId=6&action=ajax"
答案 1 :(得分:-1)
脚本文件就可以了。请更改ajax文件的条件。
$action = $_GET['action'];
if ($action == "ajax") { // if condition is met, run the script
//// Here process your code
}