-2

I've never worked with JSON from a web API before. I'm curious as to how it works. I've been reading up online but I don't really know for sure how it's done.

From what I understand it's:

1) Send a GET request to the server 2) Receive the JSON response 3) Do something with the data

The question I have is how does the server know to send back JSON instead of HTML?

Hugo
  • 1,612
  • 7
  • 24
  • 36
  • 3
    Because you make it that way. Construct the JSON response, and return it. It's not magic, you have to tell it what to do – Sterling Archer Jun 02 '14 at 18:26
  • Depends which server, each web server has different configs to set `mime` types. You must have come across `content-type="text/html" in the ` ` of webpages. well JSON is `content-type="application/json"` - the server `responds` with these content types - often simply based on the file extension `.html` , `.jpg` – Rob Sedgwick Jun 02 '14 at 18:28

2 Answers2

0

The server responds with different MIME types. If you're sending back html, then the type is usually 'text/html' and is handled accordingly by the browser. In the case where the response is JSON, the MIME type is most often 'application/json'.

What is the correct JSON content type?

Community
  • 1
  • 1
beauXjames
  • 7,565
  • 3
  • 42
  • 65
0

There isn't really a general answer here. Unless you accept "it depends on how the server-side programmer decided to implement the server". A common one is that AJAX requests (Original article here: http://www.adaptivepath.com/ideas/ajax-new-approach-web-applications/. It was originally XML-based) are responded to with JSON and regular requests are responded to with HTML.

Here's a relatively decent PHP/AJAX guide to implementing this kind of thing, which may help with understanding the full picture. http://webdevelopingcat.com/jquery-php-beginner-tutorial-ajax/

mordocai
  • 83
  • 7