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/