AJAX MVC Server returns JSON data, client gets JSON data

Source: Internet
Author: User
Tags string to json

<>

Controller controllers

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using        System.web.script.serialization;namespace mvcapplication2.controllers{public class Homecontroller:controller {        Public ActionResult Index () {return View ();            }//---------------------------------The first notation: Server returns a JSON object public ActionResult GetData1 () {            Jsonresult json = new Jsonresult (); Json. Jsonrequestbehavior = Jsonrequestbehavior.allowget; Accept the HTML GET request JSON from the client.            Data = new {code = 0, message = "Success"}; return JSON; The direct return of a JSON object to client,client through the AJAX request, the direct is a JSON object, no need to use $.parsejson (data) This way to convert the string into a JSON object, Since the fetch itself is a JSON object.        Instead of a string.            }//---------------------------------Another way to do this: the server returns a JSON object public ActionResult GetData2 () {            In fact, this method is called under the Jsonresult class: protected internal Jsonresult Json (object data); Note: JsOn () is a method in which the parameter is an object of type objects. The return value is Jsonresult type return Json (new {code = 0, message = "On"}); By testing: In such a form.        The client can only fetch data through the asynchronous post request, assuming an asynchronous get request, unable to get the data}//---------------------------------The Third Way: Server returns a JSON-formatted string            Public ActionResult GetData3 () {JavaScriptSerializer json = new JavaScriptSerializer (); var jsonobj = new {code = 1, message = "OK"}; Constructs an object string jsonstr = json. Serialize (Jsonobj); Convert this object to a JSON-formatted string return Content (JSONSTR); Returns a JSON-formatted string.

So the client passes the AJAX request. The data obtained is a JSON-formatted string, so you need to use $.parsejson (data) to convert the string to a JSON object}}}



Views View





AJAX MVC Server returns JSON data, client gets JSON data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.