flukeflume
flukeflume

Reputation: 707

Returning a Javascript object from server

Apologies for what is most likely an obvious question. In general terms, I would like write a client-side Javascript function that makes a request to my server; this would then return a Javascript object.

This seems to be a common thing, but I'm not entirely sure which techniques I should be using. Do I need to be running something like Node on my server to do this, or am I missing something basic? And is it possible to return a Javascript object directly, or would I return JSON and then convert this client-side into an object?

Googling seems to bring up a vast number of Ajax PHP \ ASP techniques, but I'm just using javascript.

Thank you very much in advance, and please do accept my apologies if it's a dense question.

Upvotes: 1

Views: 378

Answers (3)

Axel Amthor
Axel Amthor

Reputation: 11106

Loading a javascript from the server via AJAX/PHP as a kind of dynamic plug-in to the current page is an obvious technique to keep code in the page small and maintainable (and generic ...?) I use jQuery.getScript for this. It has a callback function which will be executed when the code is loaded an has been initially run. Just look at http://api.jquery.com/jQuery.getScript/

Upvotes: 0

michaelyin
michaelyin

Reputation: 83

if you send request to server, the application running at server will send back some data to browser, Json,XML,HTML, etc. what you should know is the data returned by server may have many types.

if you have no idea about the application in server, i suggest you can write some code running at server by using nodejs. in this way you do not have to study another language like php,java,python.

Upvotes: 0

T. Junghans
T. Junghans

Reputation: 11683

The server would return JSON and JSON is javascript in object notation. The difference between JSON and a javascript object, is that JSON is a standard and has strict requirements, for example that property names and values are wrapped in double quotes.

You don't need a special server to return JSON. JSON is should be treated like XML or any other data format.

Returning JSON from a server is best done with JSONP if you want to avoid cross site scripting issues. You can read more about JSONP on

Either way, JSONP or JSON and Ajax, you'll need to make sure your JSON is valid and the correct headers are sent.

Content-Type: application/json

Upvotes: 3

Related Questions