Skip to main content
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,120
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
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

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,469
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
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
  • 28.5k
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,147
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

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
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?

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
  • 46.7k
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

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

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

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,441
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
  • 135k
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
3 votes

REST API design: Best practice for POST to create an object to a list

Go for the first option. GET and POST are independent operations. In particular, whatever filtering and pagination parameters were given to the GET request for the list are not available in the POST ...
Hans-Martin Mosner's user avatar
3 votes
Accepted

REST API design: Best practice for POST to create an object to a list

Should I return only OK message when server successfully created Employee object and then GET the updated list from server? This. When you create the new Employee you should return 201 CREATED and ...
Cormac Mulhall'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
  • 78.6k
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
  • 135k
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
Accepted

Is there an advantage to using AJAX rather than a simple window.location?

The first example submits a POST request to a server and listens for a response. When the server responds, it sets window.location to the response text. The second example constructs a URL within ...
Ben Cottrell's user avatar
  • 12.1k
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
2 votes

Ajax response -- is there an agreement on the structure?

As far as I know: no. There's no generic approach or standard. Personally I make sure to always return an object, never a single value or array. This ensures compatibility. For example, if I add a ...
Mario's user avatar
  • 1,509
2 votes
Accepted

PHP Post vs JavaScript Ajax Request

What are PHP forms? Do you mean HTTP POST requests (Wikipedia, MDN)? Ajax will usually POST as well. The only difference is that Ajax runs in the background, and does not navigate to a new URL. That ...
amon's user avatar
  • 135k

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