开发者

jQuery getJSON() - What server is called?

When using PHP I can use file_get_contents or cURL to get a URL.

jQuery runs on the client

In jQuery there is a function called jQuery.getJSON(). Javascript is run on the client. What server is used for the download of the JSON code of the external URL? What information does the called URL know about? Does it know of the domain? The IP of the client user? It's a client language.

Prefered for many request

To make many requests, is it safer to do thi开发者_StackOverflows with Javascript than PHP because it runs on the every client instead of one server point?


What server is used for the download of the JSON code of the external URL?

The one that the domain name in the URL passed to that function resolves to.

What information does the called URL know about?

It is an HTTP request, like any other. The usual information will be available.

Does it know of the domain? The IP of the client user?

Of course.

It's a client language.

… making an HTTP request.

To make many requests, is it safer to do this with Javascript than PHP because it runs on the every client instead of one server point?

You control the server. You don't control the client. JavaScript can be disabled. It is safer to make the request from your server.

(For a value of "safe" equal to "Less likely to fail assuming the service you are using doesn't impose rate limiting")


Because of the Same Origin Policy all requests made in JavaScript must go to the domain from which the document was loaded. It's a standard HTTP request, so the server will have the same information it would if a user was just navigating around (including cookies, etc.) From the phrasing of your question it appears you need to make requests to some external site, in which case making those requests from your server which is not subject to such a security policy would likely be best.


In jQuery there is a function called jQuery.getJSON(). Javascript is run on the client. What server is used for the download of the JSON code of the external URL? What information does the called URL know about? Does it know of the domain? The IP of the client user? It's a client language.

The code that runs your web browser is only on your PC, too, yet it is perfectly capable of retrieving content via the HTTP protocol from a web server, and has done so for several decades.

AJAX requests are no different. jQuery creates an XMLHttpRequest object that performs an HTTP request in a manner uncoupled from the general page context. As far as the server's concerned, it's just an HTTP request like any other.

The text contents of the result you get back happen to be written in JSON format, but the HTTP layer neither knows nor cares about that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜