Home > Web Front-end > JS Tutorial > EasyUI's datagrid cross-domain call implementation code in ASP.NET MVC_jquery

EasyUI's datagrid cross-domain call implementation code in ASP.NET MVC_jquery

WBOY
Release: 2016-05-16 17:55:20
Original
1137 people have browsed it

In recent projects, I need to call data from other projects across domains. Other projects also use EasyUI's datagrid component. I initially thought that it would be enough to directly define the url attribute of the datagrid as the url address of other projects. However, after testing, I found that json data was indeed returned. However, the json data prompts an "invalid label" error. I searched online for a solution to the error. Refer to the method "JavaScript Processing Json Invalid Label Error Solution" and used the loadData method of the datagrid to load and convert the json. It still prompts the above error. I feel that the reason is not there. Format issue.

I searched for the article "JavaScript cross-domain access problem solution" and found out that it is because easyUI uses JQuery's asynchronous method to load data and should follow JQuery's cross-domain access rules. , that is, jsoncallback= needs to be added to the URL mentioned in the above article? Callback function parameters, and the format of the returned json must be modified to: callback function name (json data), and now the data returned is only data in json format without a callback function name, which naturally prompts a format error, so I modified the ASP.NET MVC auto The defined JsonResult class, for details on how to write a custom JsonResult class, see: Customizing ASP.NET MVC JsonResult serialization results,

The code is as follows:

Copy the code The 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
//Cross-domain calls need to modify the json format jsoncallback
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 of EasyUI’s datagrid is as follows:
Copy code The 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();
}
});

Author: mikel
Source :http://www.cnblogs.com/mikel/
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template