Hot answers tagged

122 votes
Accepted

What's actually wrong with an endpoint returning HTML rather than JSON data?

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 ...
Machado's user avatar
  • 4,110
51 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

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 ...
Robert Harvey's user avatar
23 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

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 ...
DeadMG's user avatar
  • 36.9k
21 votes
Accepted

Many asynchronous calls vs single call to the API

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' ...
Chris Cirefice's user avatar
17 votes
Accepted

RESTful HTTP and websocket in the same application?

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 ...
maple_shaft's user avatar
  • 26.5k
14 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

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 ...
Victor Sand's user avatar
12 votes

If I want to implement ajax, does the choice of the web server make a difference?

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 ...
Arseni Mourzenko's user avatar
7 votes

Is it safe to say that AJAX is a particular way of using HTTP?

Ajax and rest are indeed two ways of using HTTP. But you can use both together. HTTP (HyperText Transfer Protocol) is a protocol to send files (and data) from the server to the client. Originally ...
Kasper van den Berg's user avatar
6 votes
Accepted

HTTPS site perform ajax calls to native http desktop aplication

THE ORIGINAL SOLUTION IS NOT VALID ANYMORE! Updated Solution Everyone doing this (github, spotify, blizzard, dropbox, etc) have all seen their certificate revoked last year because the private key ...
Mathieu Fortin's user avatar
6 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

There are 3 ways (at least?) to build up a web page: Generate the entire page server side. Return a bare bones page from the server plus code (JavaScript), and have the page fetch its data and render ...
jpmc26's user avatar
  • 5,409
6 votes
Accepted

JavaScript only validation on AJAX form submit

The answer is 'no' as others point out but it's really important to understand why. The best way, I think is take web browser (the following assumes Chrome), point it to your web page and right click-...
JimmyJames's user avatar
  • 26.7k
6 votes
Accepted

Websockets vs Ajax for live search

You only need websockets if you want the communication to be initiated by the server, not the client. Unless I misunderstand, in your case, the client seems to initiate every communication. In that ...
Glorfindel's user avatar
  • 3,137
5 votes

Image resizing client side vs server side

If disc space is the only constraint then it doesn't matter which end resizes the image. The client might do it slightly slower than the server or the server may be overloaded with clients sending ...
gbjbaanb's user avatar
  • 48.5k
5 votes

Chat application - write to file and then save in database

The efficiency part of the question can't be answered with the given context since all we know is a client will make a request every 2.5 seconds. At a high level we don't know how many clients there ...
Cole Ole's user avatar
  • 161
4 votes
Accepted

Image resizing client side vs server side

From a speed point of view, your Computer is most likely much faster than the server you have for your website/web-application, so it would make more sense to resize on the client, than on the server. ...
XaolingBao's user avatar
4 votes
Accepted

Managing session timeouts with regards to user activity in the page

Your second solution is best solution. I believe this because it should fundamentally be the clients responsibility to inform the server that they are still active. The server can then do as it must ...
Dellowar's user avatar
  • 508
4 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

I would say there is nothing wrong with the server returning an HTML fragment and the UI assigning it to .innerHTML of some element. This is, in my opinion, the easiest way to develop asynchronous ...
juhist's user avatar
  • 2,579
4 votes

Client wants browser's back button to work on a website doing AJAX calls

My client wants the browser's back button to work, but since you're not moving to a different page or anything, I don't think that's actually possible, is it? No, it certainly is possible. When ...
Winston Ewert's user avatar
4 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

There is nothing wrong in principle. The question is: What do you want to achieve? JSON is perfect for transmitting data. If you send HTML instead and expect the client to extract the data from the ...
gnasher729's user avatar
  • 44.6k
4 votes

Better to send all data to client or query server for every search?

There is a better approach. First, do not send all the results by default in a json file. Your clients will have to download a huge file for potentially no reason at all, and you could have browser ...
Steve Chamaillard's user avatar
3 votes

Managing session timeouts with regards to user activity in the page

You shouldn't rely on Javascript as the only solution. The PHP should track the session expiration with the Javascript class handling the updates on the page. If no updates from the JS, then the PHP ...
Phil M's user avatar
  • 271
3 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

This all depends on the purpose of the API, but generally what you describe is a pretty strong violation of separation of concerns: In a modern application, API code should be responsible for data, ...
SouthShoreAK's user avatar
3 votes

Better to send all data to client or query server for every search?

2000 items isn't really very much data if each item is a name and an id (unless the names are huge). Searching through it in Javascript will be plenty fast. If compressed when sent over the HTTP ...
Ben Turner's user avatar
3 votes
Accepted

What happens if multiple users send http requests to the server concurrently?

What happens if multiple users send requests to the server concurrently? It depends on what the requests do. If they're just reads, nothing happens. If they're, say, incrementing counters, or ...
LSerni's user avatar
  • 2,431
3 votes
Accepted

An approach to send the contents of a file on the server to the client in real-time

You can use chunked transfer encoding to respond in multiple fragments. This allows you to stream the response without having to introduce drastic changes. However: This will require support from ...
amon's user avatar
  • 134k
3 votes

Server-side script to handle many near-simultaneous incoming XMLHttpRequests

This sounds like premature optimization. It is actually built into the network stack that connections queue up until the server is ready to process so no need to reinvent the wheel. Just inserting ...
Esben Skov Pedersen's user avatar
2 votes

making http request by ajax or c#

Usually the overriding reason for making a 3rd party API call server side is that it requires you to have an account, username and password with the 3rd party. If you make the call client side then ...
Ewan's user avatar
  • 74.4k
2 votes
Accepted

How to validate ajax request comes from correct page and is not tampered with?

The main concern is not XSRF, but the fact that authorised user could tamper with it, by changing a part of html code if the ID is stored visibly (or invisibly) somewhere. I understand the desire to ...
amon's user avatar
  • 134k
2 votes

Performance of frequent AJAX requests sent for database queries

Depending on the data being searched perhaps using some caching mechanism would be appropriate so that instead of hitting the database you hit the cache first and only if the data is not available in ...
DMH's user avatar
  • 86
2 votes

What's actually wrong with an endpoint returning HTML rather than JSON data?

HTML is tied to a specific design and use. With HTML, if you want to change the page layout you need to change how the html is generated by the server call. Usually, that requires a back-end ...
Joel Coehoorn's user avatar

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