Search code examples
phpjavascriptmongodbanalytics

Most efficient JavaScript for sending to PHP


I'm working on an analytics solution that stores data in a MongoDB. Currently I have a javascript which uses an XMLHTTPRequest object to send data to a PHP file using the GET method.

The PHP in turn then creates/updates a document on the MongoDB.

Is this an efficient way of doing things? Is there a better way to do it?

Whilst it's just a side project at the moment I want to try and make this a scalable solution from the outset.


Solution

  • As others have said, there's a reason to use POST because of browsers' limitations. But I'd like to make another agruement.

    POST makes more sense in terms of HTTP method definitions. GET is supposed to be safe and make no changes to your system state (database). This is typically enforced in services, but not as much in HTML form processing.

    Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as safe, which means they are intended only for information retrieval and should not change the state of the server. In other words, they should not have side effects, beyond relatively harmless effects such as logging, caching, the serving of banner advertisements or incrementing a web counter. Making arbitrary GET requests without regard to the context of the application's state should therefore be considered safe.

    Source

    Additional reading:

    HTTP Spec - Method Definitions