3

I'm looking to make an object containing latitudes and longitudes of various places such as [["Second Event",19.0554748,72.8497017],["Demo Event",19.2097381,72.8737017]].

I'm successful of making this in php by using json_encode() function. How do I retrieve it in the callback function. I've tried the following:

$.post('maps1.php',{},function(data){
    alert(data);
    markers=JSON.stringify(data);
},"json");
alert(markers);

However this doesn't seems to work. What should I do?

2
  • 2
    Can you explain "this doesn't seems to work"? Are you getting any errors? Commented Dec 18, 2012 at 6:08
  • possible duplicate of How to return the response from an AJAX call? Commented May 30, 2014 at 14:38

3 Answers 3

3

You have the scope for the variable markers inside the post method, try doing it like this:

var markers = '';

$.post('maps1.php', {}, function (data) {
    alert(data);
    markers = JSON.stringify(data);
}, "json");

alert(markers)
Sign up to request clarification or add additional context in comments.

6 Comments

@user1911914 Are you getting the alert for the data to appear?
@user1911914 To make sure your browser supports JSON parsing you can visit caniuse.com/json
the browser does support json...and im not getting the alert
@user1911914 If your not getting the alert, you need to start there. I'd recommend using Chromes inspector and opening the network tab to see if your getting a ny errors.
Its difficult to monitor using firebug since Im working on google maps api which takes a lot of data..
|
0

$.ajax should do this way:

     var marker;
     $.ajax({
        url:'maps1.php',
        data:{},
        type:'POST',
        dataType:'json',
        success:function(data){
            alert(data);
            marker = JSON.stringify(data);
        },
        complete:{
            alert(marker);
        }
   });

and $.POST should be like this:

   $.post('maps1.php',{},function(data){
       alert(data);
   },"json").done(function(data){
       markers=JSON.stringify(data);
       alert(markers);
   });

but i prefer to use $.ajax()

Comments

0

ajax string syntax below:

 var marker;
 $.ajax({
    url:'maps1.php',
    data:{},
    type:'POST',
    dataType:'json',
    success:function(data){
        alert(data);
        marker = JSON.stringify(data);
    },
    complete:{
        alert(marker);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.