??yy 2017-02-18 15:57 采纳率: 0%
浏览 35

Ajax 500服务器错误

I get:

POST http://localhost:8888/goughChallenge/wp-admin/admin-ajax.php 500 (Internal Server Error)

In the console when I click on a button... Ive looked at other threads but I can see an issue with my code. Could someone enlighten me?

$(document).ready(function() {
// When document has fully loaded

$( function() {
    var locationForm = $( '#locationForm' );
    var results = $( 'div#result' );
    var formMessage = $( 'div.form-message' );
    var inputFields = $( 'input.required' );
    var submitButton = $( 'button.btn-submit' );

    $( locationForm ).submit( function( eve ) {
        eve.preventDefault();
        eve.stopPropagation();
        // Disable button
        submitButton.attr( 'disabled', 'disabled' );
        submitButton.addClass( 'disabled' );
        var currentLoc = $( '#currentLoc' ).val();
        var destLoc = $( '#destLoc' ).val();

        $.ajax({
            type: 'POST',
            url: locationAjax.ajaxurl,
            data: {
                'action': 'locationCalculate',
                'currentLocation': currentLoc,
                'destLocation': destLoc
            },
            success:function(data) {
               jQuery('#result').html(data);
           },
           error:function(errorThrown) {
               submitButton.removeAttr('disabled');
                submitButton.removeClass('disabled');
                if ( errorThrown.responseText !== '' ) {
                    $( formMessage ).text( errorThrown.responseText );
                } else {
                    $( formMessage ).text( 'An error occured and your message could not be sent.' );
                }
                console.log( errorThrown );
               }
           })
       });
    })
});

Below is where the vars are defined

<?php
/**
 * Plugin Name: Location Distance Calculator
 * Plugin URI: https://www.tomwithers.me
 * Description: calculate the distance between two given user points
 * License: GPL3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 */
function distanceCalculator() {
    /**
    * Input form for user
    */
    ?>
    <form id="locationForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="locationForm">
        <div class="form-group">
            <label for="currentLocation">Current Location: </label>
            <input id="currentLoc" type="text" name="currentLocation" class="required" />
        </div>
        <div class="form-group">
            <label for="destLocation">Destination Location: </label>
            <input id="destLoc" type="text" name="destLocation" class="required" />
        </div>
        <div class="form-group">
            <input type="hidden" name="action" value="locationCalculate">
            <button type="submit" class="btn btn-submit">Calculate Distance</button>
        </div>
        <div id="result"></div>
        <div class="form-message"></div>
    </form>

    <?php
}
add_shortcode( 'Calculator', 'distanceCalculator');

function locationData() {
    /**
    * Pull the form fields
    */
    if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
        $currentLoc = urlencode( $_POST['']);
        $destLoc = urlencode( $_POST['']);;
        $data = file_get_contents( "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$currentLoc&destinations=$destLoc&language=en-EN&sensor=false" );
        $data = json_decode( $data );
        $time = 0;
        $distace = 0;

        /**
        * Check If form has had data entered
        */
        if ( empty( $currentLoc ) OR empty( $destLoc) ) {
            http_response_code(400);
            echo "Please fill out all fields.";
            die;
        }

        /**
        * Calculate the disatnce
        */
        foreach ( $data->rows[0]->elements as $road ) {
            $time += $road->duration->value;
            $distance += $road->distance->text;
        }

        $time =$time/60;
        //$distance = round( $distnace / 1000 );

        /**
        * Output the vaules
        */

        if ( $distance != 0 ) {
            echo "<div id='result-generated'>";
            echo "From: " . $data->origin_addresses[0];
            echo "<br/>";
            echo "To: ". $data->destination_addresses[0];
            echo "<br/>";
            echo "Time: ".gmdate("H:i", ($time * 60))." hour(s)";
            echo "<br/>";
            echo "Distance: " . $distance . " Miles";
            echo "<br/>";
            echo "</div>";
            die;
        } else {
            die;
        }
    }
}

add_action( 'wp_ajax_locationCalculate', 'locationData' );
add_action( 'wp_ajax_nopriv_locationCalculate', 'locationData' );

Im unsure of whats wrong and Ive been staring at my screen for about an hour! Any help is much appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值