Hot answers tagged

115

What's actually wrong with an endpoint returning HTML rather than JSON data? Nothing, really. Each application has different requirements, and it may be that your application wasn't designed to be a SPA. It may be that these beautiful frameworks that you cited (Angular, Vue, React, etc...) weren't available at development time, or weren't "approved" ...


61

It's not so much "fake" performance as real responsiveness. There are a number of reasons why it's popular: Internet connections are fairly reliable nowadays. The risk of an AJAX request failing is very low. The operations being performed are not really safety critical. If your emails don't get deleted on the server, the worst that happens is you have to ...


50

JSON and HTML fulfill two different semantic purposes. If you are populating a web page with data, use JSON. If you are building up a web page from portions of web pages, use HTML. They may kinda sound like they're the same thing, but they aren't, at all. For one thing, when you are building up a portion of a web page using HTML returned by the server,...


47

The same origin policy is a wholly client-based restriction, and is primarily engineered to protect users, not services. All or most browsers include a command-line switch or configuration option to to turn it off. The SOP is like seat belts in a car: they protect the rider in the car, but anyone can freely choose not to use them. Certainly don't expect a ...


33

Today, in our work as in our life, it is more important know how to find information rather than know the information itself. I mean that a good developer is a person able to find documentation, network, and that share with an open mind. I am an experienced .NET developer and, believe me, for every project I work on I have to learn new things about the ...


32

Assuming something will work and displaying an error in case it fails on the remote side is much more user-friendly than blocking the user from doing anything else until there's a response from the server. An email client is actually a great example for this: When I have a list with 5 emails and the top one is selected I expect that when hitting DEL three ...


31

Where we should put search action? In GET /search/:text. This will return a JSON array containing the matches, every match containing the album it belongs to. This makes sense, because the client may be interested not in the track itself, but the entire album (imagine that you are searching for a song which, you believe, was in the same album as the one you ...


26

I can do it using AJAX. But I can also do it using a REST call. Um, no. Those two are completely orthogonal. If you want to update your page with data you have to get from a server, you will do it using AJAX. There is no other way. And that AJAX call can use REST, or something else. My problem is i really dont get the difference between REST and an HTTP ...


22

The main problem is that it tightly couples the server to the client, who must know the HTML structure. It also makes the endpoints more difficult to re-use in new ways or new applications. Returning plain data and letting the client render it decreases coupling and increases flexibility and testability- you can run unit tests on the client for mock data, ...


16

One trait of these applications: They stay on one page. SPA = Single Page App A single-page application (SPA), also known as single-page interface (SPI), is a web application or web site that fits on a single web page with the goal of providing a more fluid user experience akin to a desktop application. In an SPA, either all necessary code – HTML, ...


15

One of the core design goals of Websockets is that it allows both HTTP and Websocket protocols to be communicated over the same port. It achieves this by explicitly requiring a client to perform a Websocket handshake with an HTTP Upgrade request. In this way the server can handle a standard HTTP request connection as well as an HTTP Upgrade request that is ...


14

The users don't care about what your software is doing behind the scenes: they want their actions to have visible impact, and to be able to work at their pace. If an action was successful on client side - like deleting emails - it's your job to make it successful on server side as well. Why did the deletion fail? If it's due to some kind of limitation (for ...


14

Option 1 (multiple async calls) is the best choice because: each individual call is its own entity, so you can retry individually if something happens to fail. In the monolithic 'one-call' architecture, if one thing fails you have to do the entire call again the server side code will be simpler: again, modularity, meaning that different developers can work ...


14

I think you have it a little backwards. You say: we have no test whatsoever, so we have no particular reason to create this JSON endpoint A reason to use a proper endpoint would be so that you could test it. I'd say that having no tests is a very good reason to start writing some. That is, if there is any logic that would be suitable to test. 200k lines ...


12

You can't. Let me rephrase: Impossible You could/should make it harder to use a token. Implementing a max-usage per token, a max life-time, ... It is however impossible to know if it is the scriptkiddie or your 'application' contacting your api. A lot of people tend to forget that it is never the aplpication calling the api, it's the application, telling ...


12

Your question shows that you misunderstood AJAX, Apache and Nginx. AJAX is simply a technique for a browser to request some information from a server without refreshing the web page. It consists of an HTTP (or HTTPS) request sent by the browser to the server. For a web server such as Apache or Nginx, it makes technically little difference if the HTTP ...


9

After working with something for some time you'll learn/know the important things automatically. There is no need to know all APIs, function parameter lists, etc. There are docs for things like that. Generally spoken, you need to know things or where to find them. For example, let's take the $.ajax() function of jQuery. I know what it does and the commonly ...


9

We can't tell you if it will cause problems because it entirely depends on what you are putting into that div, your internet connection, and what kind of load you have on the server. But you need to ask yourself, do you really need to be polling multiple php scripts every second? Is the data actually changing every second? I suspect you probably should be ...


9

I'm also somewhat skeptical that every new web app needs to be an SPA but one thing I'm 100% sold on as a generalist with the bulk of his experience on the client side is that a service-oriented architecture that hands off raw data rather than HTML to the client is the way to go whether you're loading prebuilt pages/views from the server and doing a lot of ...


8

Just a thought, perhaps you could create a list for your Ajax callback methods. You would wrap your actual Ajax callback in a method that adds that callback to a slot in an list. This list waits for callbacks in a specific order and only executes if the top element (the callback you want called first) is filled.


8

You have a few options, but before you delve in to them I'd take a very good look at the Open Web Application Security Project. OWASP has some brilliant documents and guidelines. Personally, I'd begin by conducting a full and proper code review. Rather than checking for traditional issues, take a strong look at Secure Coding practice. (Once again, OWASP has ...


7

Ajax/JSON/XHP/XHR/$insert_stupid_neologism_here is as safe as vanilla HTTP (because it is vanilla HTTP) if and only if the site is secured with SSL and you use a token system to prevent cross site request forgeries.


7

This is not a race condition. A race condition would occur when the behavior between two asynchronous threads becomes undefined. You can solve this problem by nesting the callback from the first call


7

It seems what you need is a wrapper for all the parameters that define a page (say, pageNumber, pageSize, sortType, totalCount, etc.) and use this DataRequest object as the key for your caching mechanism. From this point you have a number of options to handle the cache: Implement some sort of timeout mechanism to refresh the cache (based on how often the ...


7

The same-origin policy is enforced on the client-side. If the browser supports CORS, the server can send back headers that tell the browser to make exceptions to the same-origin policy. For example, sending the header Access-Control-Allow-Origin: www.example.com would tell the browser to allow cross-origin requests from www.example.com. Access-Control-...


6

Generally speaking callbacks are universal tool for dealing with async task's and indeed callbacks is the mechanism to handle AJAX requests in JavaScript. In the context of the callback you can even have reference to the row that is clicked(if closed in closure). You can find the following articles useful: jQuery deferred and Using Deferreds in jQuery One ...


6

This is an oversimplified answer, based on what I think your current understanding is. If it's too trivial or too complex, feel free to tell me about it and I'll expand or clarify. An AJAX request is a normal HTTP request. You are essentially requesting a resource from the web server, and technically it's exactly the same request as if you'd type the URL of ...


6

Well I think you first need to understand that AJAX and REST are not really to alternatives for your use case. AJAX stands for asynchronous javascript and XML so if you are using javascript to load data after the browser request has finished you are doing AJAX. REST on the other hand stands for Representational State Transfer which as Stefan Billet pointed ...


6

Return the harshest, unfriendliest result possible in the event of a malformed request (one that returns more data than your metering allows is malformed). I suggest returning a 4** error code. Then, also provide paging parameters, so that users may request pages. oData has this feature, for instance. Do not truncate the data silently, under any ...


6

You are thinking like a programmer. Instead, try to think like a Human Resources Director. Senior Developer to Human Resources Director: We need to hire a web developer. HRD: Okay, what skills do they need to have? SRD: Well, PHP, Javascript, MVC frameworks, git... HRD: Anything else? SRD: Well, experience with AJAX would be a plus... ...


Only top voted, non community-wiki answers of a minimum length are eligible