Organizational Research By

Surprising Reserch Topic

Question:Json Ajax PHP is NULL


I get an array with the geolocation Javascript, then I want to pass this array to a php script via ajax to process it and get an processed array back. Unfortunately it seems that the passed array to php is always NULL, I have no idea why, maybe you can help me out. Thank you!

my js/jquery:

dataArray = [];
        var lat = pos.coords.latitude;
        dataArray.push({'lat':lat});
        var lon = pos.coords.longitude;
        dataArray.push({'lon':lon});
        var identifier = 'ajax';
        dataArray.push({'identifier':identifier});
        console.debug(dataArray);

        $.ajax({
                async:   true,
                type:        'post',
                cache:       false,
                url:         'custom.php',
                data:        {myJson: dataArray},
                dataType   : 'json',
                success: function(data){
                    console.debug(data);
                    var json = $.parseJSON(data);
                    console.debug(json);
                    alert(json);
                }
        });

my php:

$JSArray['array'] = json_decode($_POST['myJson'], true);
$_SESSION['jsonArray'] = $JSArray['array'];
var_dump($_SESSION);

The Result is always ["jsonArray"]=> NULL

asked Sep 13, 2013 in jquery by anonymous
edited Sep 12, 2013
0 votes
27 views



Related Hot Questions

4 Answers

0 votes

There are multiple points are which your script can be failing. I would test each step and see where it fails (by commenting out the echo statments one at a time):

echo $_POST['myJson'];
$JSArray['array'] = json_decode($_POST['myJson'], true);
//echo $JSArray['array'];
$_SESSION['jsonArray'] = $JSArray['array'];
//var_dump($_SESSION);

Also you ajax call should contain alert(data) without parseJSON, since the server is not sending JSON back, it is just echoing out a var_dump:

$.ajax({
                async:   true,
                type:        'post',
                cache:       false,
                url:         'custom.php',
                data:        {myJson: dataArray},
                dataType   : 'html', // change back to json when you send json from server
                success: function(data){
                    console.debug(data);
                    //var json = $.parseJSON(data);
                    //console.debug(json);
                    //alert(json);
                    alert(data);
                }
        });

 

answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes
0 down vote
    

try this one. it works for me

 (function($){
if(navigator.geolocation){return;}
var domWrite = function(){
        setTimeout(function(){
            throw('document.write is overwritten by geolocation shim. This method is incompatible with this plugin');
        }, 1);
    },
    id = 0
;
var geoOpts = $.webshims.cfg.geolocation.options || {};
navigator.geolocation = (function(){
    var pos;
    var api = {
        getCurrentPosition: function(success, error, opts){
            var locationAPIs = 2,
                errorTimer,
                googleTimer,
                calledEnd,
                endCallback = function(){
                    if(calledEnd){return;}
                    if(pos){
                        calledEnd = true;
                        success($.extend({timestamp: new Date().getTime()}, pos));
                        resetCallback();
                        if(window.JSON && window.sessionStorage){
                            try{
                                sessionStorage.setItem('storedGeolocationData654321', JSON.stringify(pos));
                            } catch(e){}
                        }
                    } else if(error && !locationAPIs) {
                        calledEnd = true;
                        resetCallback();
                        error({ code: 2, message: "POSITION_UNAVAILABLE"});
                    }
                },
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes

There are multiple points are which your script can be failing. I would test each step and see where it fails (by commenting out the echo statments one at a time):

echo $_POST['myJson'];
$JSArray['array'] = json_decode($_POST['myJson'], true);
//echo $JSArray['array'];
$_SESSION['jsonArray'] = $JSArray['array'];
//var_dump($_SESSION);

Also you ajax call should contain alert(data) without parseJSON, since the server is not sending JSON back, it is just echoing out a var_dump:

$.ajax({
                async:   true,
                type:        'post',
                cache:       false,
                url:         'custom.php',
                data:        {myJson: dataArray},
                dataType   : 'html', // change back to json when you send json from server
                success: function(data){
                    console.debug(data);
                    //var json = $.parseJSON(data);
                    //console.debug(json);
                    //alert(json);
                    alert(data);
                }
        });

 

answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes
0 down vote
    

try this one. it works for me

 (function($){
if(navigator.geolocation){return;}
var domWrite = function(){
        setTimeout(function(){
            throw('document.write is overwritten by geolocation shim. This method is incompatible with this plugin');
        }, 1);
    },
    id = 0
;
var geoOpts = $.webshims.cfg.geolocation.options || {};
navigator.geolocation = (function(){
    var pos;
    var api = {
        getCurrentPosition: function(success, error, opts){
            var locationAPIs = 2,
                errorTimer,
                googleTimer,
                calledEnd,
                endCallback = function(){
                    if(calledEnd){return;}
                    if(pos){
                        calledEnd = true;
                        success($.extend({timestamp: new Date().getTime()}, pos));
                        resetCallback();
                        if(window.JSON && window.sessionStorage){
                            try{
                                sessionStorage.setItem('storedGeolocationData654321', JSON.stringify(pos));
                            } catch(e){}
                        }
                    } else if(error && !locationAPIs) {
                        calledEnd = true;
                        resetCallback();
                        error({ code: 2, message: "POSITION_UNAVAILABLE"});
                    }
                },
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013

...