An error occurred while using JSON as actionresutl.

Source: Internet
Author: User

System. invalidoperationexception: this request has been blocked because sensitive information cocould be disclosed to third party Web sites when this is used in a GET request. To allow GET requests, set jsonrequestbehavior to allowget.

According to the error message, MVC2 prevents jsonresult data from being returned through GET requests by default. You can pass in the second jsonrequestbehavior parameter when returning JSON. allowget, such as: Return JSON (result, jsonrequestbehavior. allowget), of course, you can also modify your front-endCode, Use post to obtain data.

The above is a reference to the second parameter jsonrequestbehavior in the return value of jsonresult written by a dude on the net. allowget, many of my friends don't understand what it means. I didn't understand it at first. Later I thought about it for a long time before I realized it was actually very simple. Let's take an example: A jsonresult and a class

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. MVC;
Using System. Web. UI. mobilecontrols;

NamespaceMvcapplication1.controllers
{
Public ClassTestjsoncontroller: Controller
{
//
//Get:/testjson/

PublicActionresult index ()
{
ReturnView ();
}

Public Jsonresult JSON ()
{
List
< Address > Data =   New List < Address > {
New Address {ID = 1 , Addresses = " Jiangsu " , Parentid = 0 },
New Address {ID = 2 , Addresses = " Zhejiang " , Parentid = 0 },
New Address {ID = 3 , Addresses = " Fujian " , Parentid = 0 },
New Address {ID = 4 , Addresses = " Suzhou " , Parentid = 1 },
New Address {ID = 5 , Addresses = " Changzhou " , Parentid = 1 },
New Address {ID = 6 , Addresses = " Wuxi " , Parentid = 1 }
};
Return JSON (data );
}

Public   Class Address
{
Public   Int Id { Get ; Set ;}
Public   String Addresses { Get ; Set ;}
Public   Int Parentid { Get ; Set ;}
}
}
}

 

Below are the HTML and JavaScript code

 

<% @ Page Language = " C # " Inherits = " System. Web. MVC. viewpage "   %>

<!Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HtmlXmlns= "Http://www.w3.org/1999/xhtml">
<HeadRunat= "Server">
<Title>Index</Title>

<ScriptType= "Text/JavaScript"SRC= "Http://www.cnblogs.com/Scripts/jquery-1.4.1-vsdoc.js"> </Script>

< Script Type = "Text/JavaScript" >
$ ( Function (){
// $. Getjson ("<% = URL. Action (" JSON ") %>", function (data ){
// $ ("# Address"). Empty ();
// VaR html = "";
// $. Each (data, function (entryindex, entry ){
// If (entry ["parentid"] = 0)
// {
// HTML + = entry ["addresses"] + "";
// }
// If (entry ["parentid"] = 1)
// {
// HTML + = "<br> ";
// HTML + = entry ["addresses"] + "";
// }
// });
// $ ("# Address"). append (HTML );
// });
$. Ajax ({
Type: ' Post ' ,
URL: ' <% = URL. Action ("JSON") %> ' ,
Contenttype: ' APP/JSON; charset = UTF-8 ' ,
Datatype: ' JSON ' ,
Data: ' {} ' ,
Success: Function (Data ){
$ ( " # Address " ). Empty ();
VaR Html =   "" ;
$. Each (data, Function (Entryindex, entry ){
If (Entry [ " Parentid " ] = 0 )
{
Html + = Entry [ " Addresses " ] + "   " ;
}
If (Entry [ " Parentid " ] = 1 )
{
Html + = " <Br> " ;
Html + = Entry [ " Addresses " ] + "   " ;
}
});
$ ( " # Address " ). Append (HTML );
Alert (data [ 0 ]. Addresses );
}, Error: Function (Xhr ){
Alert ( ' If an error occurs, the server returns the following error message: '   + Xhr. statustext );
}
});

});
</Script>

</Head>
<Body>
<DivID= "Address">
</Div>
</Body>
</Html>

 

Is an error map without the jsonrequestbehavior. allowget parameter.

 

I have not added jsonrequestbehavior TO THE JSON return value. allowget, this parameter, but reading JSON is also normal. This proves that MVC2 is not included in this parameter for security reasons by default. for fear of directly accessing JSON through URL, it can be downloaded, added jsonrequestbehavior. you can use the getjson () or Ajax methods in jquery to use the allowget parameter.

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.