2

I have a function

 var url = "MyAvailability.aspx?mode=get";

        $.get(url,
        function(data) {
            alert(data);
        });

that returns a Json string representation

events: [{'id': 1,'start': new Date(year, month, day, 12),'end': new Date(year, month, day, 13, 30),'title': 'Lunch with Mike'},{'id': 2,'start': new Date(year, month, day, 14),'end': new Date(year, month, day, 14, 45),'title': 'Dev Meeting'},{'id': 3,'start': new Date(year, month, day + 1, 17),'end': new Date(year, month, day + 1, 17, 45),'title': 'Hair cut'},{'id': 4,'start': new Date(year, month, day - 1, 8),'end': new Date(year, month, day - 1, 9, 30),'title': 'Team breakfast'},{'id': 5,'start': new Date(year, month, day + 1, 14),'end': new Date(year, month, day + 1, 15),'title': 'Product showcase'},{'id': 6,'start': new Date(year, month, day, 10),'end': new Date(year, month, day, 11),'title': 'I'm read-only',readOnly: true'}]';

If i do alert(data);

it works but if i am trying to assign the data to a variable it fails.

my aim is to return from the function something like

 return {
        events: [
        {
            "id": 1,
            "start": new Date(year, month, day, 12),
            "end": new Date(year, month, day, 13, 30),
            "title": "Lunch with Mike"
        },
        {
            "id": 2,
            "start": new Date(year, month, day, 14),
            "end": new Date(year, month, day, 14, 45),
            "title": "Dev Meeting"
        },
        {
            "id": 3,
            "start": new Date(year, month, day + 1, 17),
            "end": new Date(year, month, day + 1, 17, 45),
            "title": "Hair cut"
        },
        {
            "id": 4,
            "start": new Date(year, month, day - 1, 8),
            "end": new Date(year, month, day - 1, 9, 30),
            "title": "Team breakfast"
        },
        {
            "id": 5,
            "start": new Date(year, month, day + 1, 14),
            "end": new Date(year, month, day + 1, 15),
            "title": "Product showcase"
        },
        {
            "id": 6,
            "start": new Date(year, month, day, 10),
            "end": new Date(year, month, day, 11),
            "title": "I'm read-only",
            readOnly: true
        }
    ]
    };

but i am not able to

if i do return data doesn t work

could you help me please i am getting crazy on this

3 Answers 3

3

Looks like you miss specification of a data format in your .get call. Please try this one:

$.get(url,
function(data) {
    alert(data);
},"json");

or consider using .getJSON method from a jquery

Sign up to request clarification or add additional context in comments.

2 Comments

When I use the above function the function(data) is skipped. Do i have to import any library to use Json?
What does it mean function is skipped? Do you get javascript errors?
1

Your question isn't too clear, just saying "it fails" or "it doesn't work" doesn't help much. However, if you're returning a json response to the browser, you should:

  1. Use the $.getJSON method, instead of $.get, OR

  2. Set the datatype of your $.get request to "json".

If you don't do either of these things, you'll need to manually parse the returned string into an object by doing:

var myObj = eval('(' + data + ')');

where the data variable holds the returned json string.

Comments

0

The problem is not with the return statement, really, but with the fact that the function it's in is called asynchronously. Thus if you put your $.get() call in a function, then that function will return before that callback function is invoked.

Instead of working with a return value, have the function itself do the work (or call some other function to do the work).

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.