How does ASP.NET MVC communicate with an AJAX enabled web page?

How does data flow from MVC to browser and back? Is it using Microsoft proprietary technologies like ASMX or WCF, or something else entirely?

It looks like MVC is using the ASMX web service they are using, but I cannot find any documentation that gives a real answer.

0


source to share


2 answers


The data from the MVC app browser ↔ is just plain HTTP request / response data. To find out what the raw data is, install Firebug or Fiddler on your PC and use that to show you the raw data and data. This is all pretty simple.

WebForms uses this same request / response model. the browser transmits some information to the web server (i.e. request ... eg .. i want to see http://www.mysite.com/foo ) and on the internet the server responds with some html, json, xml, binary data (for images) etc. this is the answer.

All browsers talk to all websites using this request / response model.



Now .. the difference with MVC and WebForms is HOW the web server handles the request and how it generates the response. So they both stick to the same concept, just handle it differently. For example, MVC uses controllers to determine what to display to the user, while WebForms have "pages" that determine what data (for that page) should be displayed.

So - essentially - you are programming your site to say:

  • If the user goes here, show them this data.
+2


source


AJAX requests are made on the page using a regular HTTP request / response. That is, in javascript, the client will create an AJAX request object, send it to a URL, and return a string. If this string is json, it can be eval'd and become a live javascript object.

The philosophy behind MVC is that all HTTP requests go through controllers. WCF is only for other types of web services where the client does not consume html-json-css-etc.

You can return JSON from a controller action using the Json (Object Model) method in System.Web.Mvc.Controller.

eg



ActionResult MyAction() {
    return Json(new { success=false, for_lunch="mmm, chicken"});
}

      

This will return json that your webpage can use. So this leaves the question - how does the browser call MyAction for the json?

There are several posts in this thread and the first thing I can find was this post .

Hope it helps

+1


source







All Articles