Cross-origin call implementation code of datagrid in EasyUI in ASP. NET MVC

Source: Internet
Author: User

The data of other projects needs to be called across domains in recent projects. The datagrid component of EasyUI is also used in other projects. You can define the url attribute of the datagrid as the url address of another project directly, however, the test showed that json data was indeed returned, but the json data prompts "invalid label" error. The solution was found online, refer to the "JavaScript processing Json invalid label error solution" method. Use the loadData method of the datagrid to load and convert the json file. The above error is returned, and the cause is not the format problem.

I searched for the JavaScript cross-origin call article "How to Solve JavaScript cross-origin access problems" and found that easyUI used JQuery's Asynchronous Method to load data, the cross-origin access rules of JQuery should be followed, that is, jsoncallback =? Must be added to the url mentioned in the above article? The callback function parameter and the returned json format must be changed to: callback function name (json data). Currently, the returned data is only json data without the callback function name. A format error is prompted, so I modified ASP.. net mvc custom JsonResult class. For details about how to write custom JsonResult classes, see: Custom ASP. net mvc JsonResult serialization result,

The Code is as follows:
Copy codeThe Code is as follows:
Public class CustomJsonResult: JsonResult
{
Public override void ExecuteResult (ControllerContext context)
{
If (context = null)
{
Throw new ArgumentNullException ("context ");
}

HttpResponseBase response = context. HttpContext. Response;

If (! String. IsNullOrEmpty (ContentType ))
{
Response. ContentType = ContentType;
}
Else
{
Response. ContentType = "application/json ";
}
If (ContentEncoding! = Null)
{
Response. ContentEncoding = ContentEncoding;
}
If (Data! = Null)
{
# Pragma warning disable 0618
// Modify the json format jsoncallback for cross-origin calls.
If (context. HttpContext. Request. Params. AllKeys. Contains ("jsoncallback "))
{
String callback = context. HttpContext. Request. Params ["jsoncallback"];
Response. Write (callback + "(" + JsonConvert. SerializeObject (Data) + ")");
}
Else
{
Response. Write (JsonConvert. SerializeObject (Data ));
}
# Pragma warning restore 0618
}
}
}

The code for the EasyUI datagrid is as follows:
Copy codeThe Code is as follows:
// Datagrid
$ ('# Dg'). datagrid ({
Url: 'http: // localhost: 9000/ManagementSystem/ListCurrent? Department = sss & jsoncallback =? ',
PageNumber: 1,
PageSize: 20,
PageList: [20, 40, 60, 80,100],
OnDblClickRow: function (rowIndex ){
Edit ();
}
});

By mikel
Source: http://www.cnblogs.com/mikel/

Related Article

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.