繁体   English   中英

带有结果处理程序的简单 Ajax 调用不起作用

[英]Simple Ajax call with result handler does not work

我得到了这个 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 中的目的地或原点值。我做错了什么?

错误来自 URL 中缺少的/ 它被重定向,POST 数据在这个过程中丢失了。

正确网址:

url:"/index.php?option=com_rsform&formId=6&action=ajax"

脚本文件没问题。 请更改ajax文件条件。

$action = $_GET['action'];

if ($action == "ajax") { // if condition is met, run the script


     //// Here process your code
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM