开发者

How to use Json string in javascript

开发者 https://www.devze.com 2023-01-04 04:16 出处:网络
I have a function var url = \"MyAvailability.aspx?mode=get\"; $.get(url, function(data) { alert(data); });

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, m开发者_JAVA技巧onth, 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


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


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号