Click here to Skip to main content
15,891,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a javascript function which return data.
The function is

JavaScript
function getEventData() {

      return { events: [{ "id": 2, "start": new Date(2013, 4, 24, 12, 0), "end": new Date(2013, 4, 24, 11, 0), "title": "test"}] };

}

it's working fine when I write the function like above (with hardcore data)

When I use some variable then it's not working ..like

JavaScript
function getEventData() {

        $.ajax({
                   url: "../PHP/PhpAction.php?f=fetchCalendarEvent",
                   async: false,
                   success: function (data) {
                       result = data;
                   }
               });
            alert(result);
            return result;
       }


But in the Alert the same data is coming.If I just copy the alert data and paste in return it's working fine

Can any body help me please....
Posted
Updated 28-Apr-13 10:46am
v2
Comments
Sergey Alexandrovich Kryukov 28-Apr-13 15:33pm    
When you copy the data from alert and paste it, you get not the same, but the string representation of return object, not the object itself. Are you getting the idea?
—SA
amit_ghosh18 28-Apr-13 16:09pm    
yes I understand that..But do you have any solution for that.

Hello Amit,

Try adding dataType:"json" in the ajax call. It should return the JSON object instead of a string. Your modified code will look something like the one shown below.
JavaScript
 function getEventData() {
    result = "";
    $.ajax({
        url: "../PHP/PhpAction.php?f=fetchCalendarEvent",
        dataType: "json",
        async: false,
        success: function (data) {
            result = data;
        }
    });
    alert(result);
    return result;
}

Regards,
 
Share this answer
 
SQL
Please check the return value in server side that it is in json format or not and also use
dataType: "json" in client side.I think the server side data may not have proper json format.
Then you can try json_encode in the sever side make a json string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900