The "{lat=xxx, lon=xxx}" must a typo? That is neither JSON nor JavaScript.
Proper JSON would look like this (the whitespace is optional):
{ "lat": 40.0, "lng": -120.0 }
BTW, the quotes on the property names are required for correct JSON format,
although they don't matter if you're just generating executable JavaScript.
By adding the variable assignment, you've changed the response from JSON to
executable JavaScript code.
However, that's not a complete solution. How do you know when your script
has loaded? You don't get notified that a variable has been assigned into.
That's why JSONP uses a callback, not a variable assignment. If the server
response is:
mycallback({ "lat": 40.0, "lng": -120.0 })
Then you *know* when it's loaded, because your "mycallback" function is
called at that time.
Now you don't have to use a timer (BAD idea!). You don't have to worry about
readystatechange. You don't have to do any of that. You simply have a
function in your JavaScript code that *automatically* gets called at exactly
the right moment.
As I mentioned in my other message, the customary way to do this is to
provide a callback function name in the URL, such as:
http://example.com/myjsonservice?foo=bar&abc=xyz&callback=callme
Then the server would generate:
callme({ "lat": 40.0, "lng": -120.0 })
This places control of the actual function name in the client code.
You don't *have* to do that; you can have a hard coded callback function
name if you want. But the conventional JSONP approach used by many sites
lets the client specify the callback name. Most JavaScript libraries such as
jQuery now have built-in support for JSONP with a callback - it's really the
way to go.
-Mike
> From: Neil.Young
>
> Exactly. The "straight" JSON, delivered until now by my
> server, didn't work the very first attempt. Reason: I was
> missing the assignment statement, so the plain JSON object
> "{lat=xxx, lon=xxx}" had to be delivered padded with a
> variable assignment, e.g. "data = {lat=xxx, lon=xxx}" by the server.
>
> But Micheal: Why this?
>
> JSON is not executable JavaScript and cannot be used
> cross-domain without a proxy.
>
>
> I think, "cross-domaining" is of course possible, but it is
> probably hard to deal with the results? The server side is
> called in any case...
> Or do I catch that wrong? For me XMLHttpRequest is not cross
> domain and cannot be used w/o proxy, that's for sure.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---