Jsonresult Usage of MVC

Source: Internet
Author: User

Because of the protection of data in ASP. 2, the request is post by default, so it is required to post requests when the front-end is requested

Action method:

 Public jsonresult getpersoninfo ()         {             varnew             {                 " Zhang San ",                   A ,                  " male "              };              return Json (person);         }

Front-End Request code:

$.ajax ({                           "/friendlink/getpersoninfo",            "POST " ,           "json",            data: {},           success: function (data) {                        $ ("#friendContent"). HTML (data. Name);            }         )

However, if you change the Get method request, you will get an error, such as:

Isn't it possible to request it in a get way?

Of course it can be, it's simple.

The JSON method has a refactoring:

protected internal Jsonresult Json (object data);
protected internal Jsonresult Json (object data, jsonrequestbehavior behavior);

We just need to use the second type, plus a JSON request behavior is OK for Get mode

 Public jsonresult getpersoninfo ()         {             varnew             {                 " Zhang San ",                   A ,                  " male "              };              return Json (person,jsonrequestbehavior.allowget);         }

In this way we can request it in the front-end using get:

$.getjson ("/friendlink/getpersoninfo"null, function (data) {                 $ ("#friendContent"). HTML (data. Name);            })

So that we can serialize the objects we want to return through JSON (return jsonresult), we don't have to use JavaScriptSerializer to serialize, MVC has helped us with these, isn't it easier now!

Jsonresult Usage of MVC

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.