I am fairly new to web development, and I am currently creating a mobile web app with PhoneGap. That is to say, it's done entirely in html and associated technologies, which the app then links up properly to function within whatever OS the device is running.
Portions of this app require me to retrieve data from a database on a remote server. This is an entirely new thing to me, having never done any programming involving networks/connections.
I was recommended to use AJAX for this purpose, but I'd like to be certain I'm making an informed choice. What other options are there for an HTML page to retrieve data from a remote server?
there is no other choice you will have to use ajax ( using jquery for exqmple ) to get your data back from the server ..
the best way ( from my point of view ) is to make sure that your data is retrieved as json or jsonp so that you can handle it easily within your app.
and if you do use jquery ajax remember to configure it to allow external ajax request.
Related
Let's say I am creating a webapp for a library. My base url is http://mylibrary.com. I want to use "pretty" URLs as follows:
http://mylibrary.com/books (list all books)
http://mylibrary.com/books/book1 (details of a particular book)
At present my approach is to create a single page app and use history api to manage the URLs. i.e I load all CSS and JS files when the user visits the home page. From then I just get data from server using AJAX, in JSON format and then create the required HTML using Javascript.
But I have learnt that this is not so good from SEO point of view.If a crawler were to visit http://mylibrary.com/books it will not see booklist at all because AJAX calls would not take place.
My question is what is the other approach to design this kind of app ? Specifically:
Should the server create entire web page and send it to browser? I mean will the response from server include everything from <html> to </html> or only the required parts?
Do programming languages like PHP efficiently manage to send the HTML to clients ? I would rather have the webserver do it ..
It appears to me that in this scenario AJAX would have very little role to play other than may be change minor parts of the page. Is that a correct understanding ? ..and here I was thinking AJAX is the modern way of doing things
A library would have many books.
So the list would be long..
Using ajax allows you to fetch only the part of it the user is trying to read, without having to retrieve the entire list, or navigate by reloading.
so for low bandwidth, and impatient users, ajax is a godsend.
for crawlers that need the entire page to collect data from, not so much..
so really you want to provide different content depending on the visistor.
How to identify web-crawler?
IMHO: Provide the page from php, if the user agent is a robot, provide the list, otherwise provide the fancy ajax based site, that shows only what you want, when you want..
I'm confused about node.js and ajax. Can you guys give me the difference.
The only similarity between AJAX and Node.js is that they are both JavaScript; yet they both serve completely different purposes.
AJAX (short for Asynchronous Javascript and XML) is a client-side technology, often used for updating the contents of a page without refreshing it. Such use can be seen just about everywhere; from Facebook, even to Stack Overflow.
Node.js is server-side JavaScript, used for developing server software. For example, you wouldn't want to develop a database system in client-side JS (since, well, anyone could access the database's credentials obviously), but such a task is possible with Node.js. Node.js doesn't get executed in a browser, but by a server - it's uses are very comparable to Apache/PHP, Ruby on Rails, Django, and the like.
TL;DR: AJAX is for client-side stuff, Node.js for server-side stuff
In easy term, The difference between Node.js and Ajax is that, Ajax (short for Asynchronous JavaScript and XML) is a client side technology, often used for updating the contents of the page without refreshing it. While,Node.js is Server Side JavaScript, used for developing server software. Node.js does not execute in the browser but by the server.
Node is full development environment allowing you to thing normal js cannot not like communicate with databases. Ajax is just a technique to get data or scripts from an external file.
I have been looking at Hulu's new website and I am very impressed from a developer's standpoint (as well as a designer's).
I have found that, unless you switch between http/https, you are served content entirely from json requests. That is a HUGE feat to have this level of ajax while maintaining browse back button support as well as allowing each url to be visited directly.
I want to create a website like this as a learning experience. Is there any type of framework out there that can give me this kind of support?
I was thinking I could...
leverage jQuery
use clientside MVVM frameworks like KnockoutJS?
use ASP.NET MVC content negotiation to serve html or json determined by an accept header.
using the same codebase.
use the same template for client side and server side rendering
provide ways to update pagetitle/meta tags/etc.
Ajax forms/widgets/etc would still be used, by I am thinking about page level ajax using json and client side templates.
What do you think? Any frameworks out there? Any patterns I could follow?
It is always best to first build a website without AJAX support, then add AJAX on top of that. Doing this means that:
users without javascript can already access your website
users can already visit any URL directly.
Adding AJAX support can be accomplished by various javascript libraries. So that you can render json content, you will want to look at javascript templating. You will want to use javascript templating even on your server side for when you add AJAX support (file extension .ejs). This will probably require some appropriate libraries to run javascript on the server.
When you add AJAX support, you will want to use the "History.js" library for browser back/forward/history support.
Make no mistake. This is a HUGE project (unless your website only has a few pages). So it is going to take a LONG time to add all the AJAX support to the best possible standard.
to answer your bullet point about using the same template server side as well as client side: check outdust. It was originally developed by akdubya, but has since been adopted and enhanced by linkedin. They use it to render templates on their mobile app client side. Personally I've used it on the server side and it works great.
As part of a product we are deploying, clients need to access a remote API on our servers to access content and data. Nonetheless, for some reasons and some clients, a solution where the entire page is on our servers is not desireable (reasons include: control over design, but mostly SEO, and them wanting this content to be available under "their domain")... A script that accesses the API server-side is not desirable due to other issues.
My idea follows (and I will point out its flaws so others can please suggest alternatives):
1) Make a simple script to be hosted on the clients server which will obtain all traffic from a certain URI path (catch-all script, similar to any framework router). so /MyApp/*. This script would always return a single code, a "loader javascript and styling"...
2) Through javascript returned from the script above, extract the URL, and process the URI after the desired path /MyApp/[*] and send it to an external call with JSONP or CORS regular ajax, the return is then styled appropriately and displayed.
With this, a url such as /MyApp/abc and /MyApp/def would have the same html/js in the browser source, but the JS would load different data from the ajax call, therefore showing different content...
This would seem like a good solution, the only drawback is that from my understanding, google and other searchengines wouldnt ever be able to access the content from abc and def, they would only access the "loader javascript and styling" (obvious enough, they arent going to be running the JS)...
So this is better than #! in that it wouldnt screw with URLs, but would still be depending on JS, so not search engine friendly...
Due to server restrictions, I'd much rather have a simple "catchall" page, and have the API called from the client-side than have to impose minimum requirements such as curl, etc... plus I'd have access to the end-user ip address more easily this way (although I could make a more elaborate proxy - which would make installing it much harder on clients' servers)...
Is there a way of achieving this without conneting to the api from the server-side?
The easiest method of doing this IMO is to have an AJAX controller (assuming MVC design) to handle all remote requests. Have each action in your controller return JSON, and then you have easy access to the data with a serverside call.
Otherwise you are using the #! solution (which you don't like, and rightly so..), or using JSONP (a hassle as well).
I'm continuing my research on solutions for our organization for our mobile app strategy. There's two somewhat conflicting requirements:
it should be a web app in that all updates for users are immediate as we update the app on the server
the app should leverage native UI widgets and native device features as much as possible
With PhoneGap, the process appears to be that you'd normally keep the UI on the device, as part of the compiled app, and you'd hit your server via AJAX to deal with the data updates and submissions. That then gives you access to the native device's UI widgets and features using the PhoneGap API into their hooks.
However, in doing that, we lose bullet point one.
My question is if one can, using PhoneGap, create a 'web view' within the app that would allow us to keep the UI on our server (the app then pretty much becomes a custom browser). And, if so, could our UI still access the device's native functionality via PhoneGap's API or is that only accessible if we compiled our UI code?
A similar question came up on the PhoneGap Google Group recently.
One thing that came up was that you are unlikely to pass app store certification if you are loading UI and core functionality from a remote server as this invalidates much of the purpose of certification.
If you're going to do this, you should set up web services that return the content you want to display to users. Then you can use jQuery Mobile and PhoneGap to take the content returned from the server and display it on the application with the appropriate native UI widgets and features that you want.
So use jQuery Mobile to hit your server for data using something like:
var fileUrl = "http://www.mywebserver.com/servedevice?data=current&selection=fresh";
$.ajax({
url: fileUrl,
dataType: "html",
success: function( html ) {
// If the call succeeds, do something with the return value here
}
});
There are many other ways you can handle it including JSONP methods. See the documentation for details:
http://api.jquery.com/jQuery.ajax/
Another approach would be to point your HTML file at a server for the JavaScript, just like a CDN file load. Then, your app's JavaScript would be loaded at runtime instead of bundled into the app. However, when it actually runs, it is running within the PhoneGap WebView so it has access to all of the PhoneGap APIs.
This is the jQuery Mobile example:
<script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>