NickyMan
NickyMan

Reputation: 39

Unable to retrieve ajax post data

I'm new to jquery, I learn this from other site, I'm trying to receive the post values from the ajax but it doesn't receive anything. Here is my code:-

index.html

<form id="refresh_form" name="refresh_form">
    <input type="hidden" name="minlat" id="minlat" value="1">
    <input type="hidden" name="minlong" id="minlong" value="1">
    <input type="hidden" name="maxlat" id="maxlat" value="1">
    <input type="hidden" name="maxlong" id="maxlong" value="1">
    <button id="refreshbutton" onclick="callAPI();">Refresh</button>
</form>

<script>
function callAPI() {
$.ajax({
        type: 'POST',
        url: 'getData.php',
        dataType: 'json',
        data: $("#refresh_form").serialize(),
        //data: postForm,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
            // Display a map on the page.  Obviously this needs to be done only once.
            if (firstTimeLoaded == false) {
                firstTimeLoaded = true;
                mapOptions = {
                    mapTypeId: 'roadmap'
                };
                map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
                map.setTilt(45);

            }
            initialize(data);
        }
    });
}
</script>

getData.php

if(isset($_REQUEST['minlat'])){
    $data = $_REQUEST['minlat'];

    echo json_decode($data);
    exit;
}

Any reason why it didn't work? From my console, I see this data is being sent.

enter image description here

enter image description here

Upvotes: 1

Views: 148

Answers (3)

Haresh Vidja
Haresh Vidja

Reputation: 8496

You code looks fine, please test on server side $_POST variable is filled up or not

<?php
print_r($_POST);
die();

Check it in response of network tab in browser's inspect.

If still not working then check .htaccess fil, Check in your .htaccess file for redirect rule is written or not. if redirect rule is there then remove it.

Upvotes: 1

Abhishek Dhanraj Shahdeo
Abhishek Dhanraj Shahdeo

Reputation: 1356

I am not sure as I haven't checked it yet, but try this one:

function callAPI() {
$.ajax({
    type: 'POST',
    url: 'getData.php',
    dataType: 'json',
    data: {"minlat":$("#refresh_form").serialize()},
    //data: postForm,
    cache: false,
    contentType: false,
    processData: false,
    success: function(data) {
        // Display a map on the page.  Obviously this needs to be done only once.
        if (firstTimeLoaded == false) {
            firstTimeLoaded = true;
            mapOptions = {
                mapTypeId: 'roadmap'
            };
            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            map.setTilt(45);

        }
        initialize(data);
    }
});
}

Upvotes: 0

MaximeK
MaximeK

Reputation: 2071

You need to use that

if(isset($_POST['minlat'])){
    $data = $_POST['minlat'];

    echo json_decode($data);
    exit;
}

Since its type: 'POST'

Upvotes: 0

Related Questions