PDA

View Full Version : how api works client side



morplug
10-02-2011, 06:58 PM
I have been interested in APIs in the last time and i want to know exactly how it works from the server side.

so i tried to figure out how the client side API working ,
first step i took the Google Plus One JS client side code and i tried to figure out how it works , mostly i tried to find how it gets connect to the server side.
i couldn't find what this whole code doing , mainly because i focused to find Ajax requests , but there is none of them.

this is the api I checked :

https://apis.google.com/js/plusone.js

now , my questions is simple ,
i want to know how API connect to the server side , do they use AJAX?
what exactly they use to send request to server side ?


i need you're help to know more on how api working on Client side , mainly on the connect to the server.
so , how it get's done?

djr33
10-02-2011, 07:19 PM
Browsers block cross-domain requests, so this is a major problem for an API. This means that Ajax and other methods won't work.

The usual way around this is to use a .js file, which CAN be loaded from a different domain. If this file is generated dynamically, then it can include any information that is needed. If only information is needed (not Javascript code), then usually the JSON format is used. To get around the security issues, JSONP is a special variation that contains instructions to execute the information (through a callback function) when it is retrieved. For more information, look into that.

There are other ways to build APIs, but in Javascript that's one of the only workable options when you must use cross-domains. You can also use a serverside language as an intermediate step where the security restrictions won't exist, but then every request will be coming from your server and that can lead to being denied requests by the API server if you have too many users.

morplug
10-02-2011, 08:37 PM
"If this file is generated dynamically, then it can include any information that is needed"
what do you mean?
i quite did not understand you , what i'm looking for is how this api is sending a server request , you may have tutorials? thank you.

djr33
10-03-2011, 02:39 AM
Just like a .htm or .php file, a serverside language can generate a dynamic (changing) .js (Javascript) file. In this you can include information such as variables or arrays. That is how you can communicate with the server.

The browser requests "somerequest.js".
The server sends a few variables (for example, map coordinates) in that Javascript file.
The browser then receives this information and can use it.


As I said above, look into JSONP. This is as far as I know the main way that APIs work with Javascript.
I don't know all of the details and there is no reason for me to try to explain it here: a quick search will give you a lot of information. It's complicated, but it's a good option if you have access to both servers. If you don't have access to one server or the other it won't work: both must be coordinated.