??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 关于#java#的问题:找一份能快速看完mooc视频的代码
    • ¥15 这种微信登录授权 谁可以做啊
    • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
    • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!