C#WebMethod未返回JSON对象
我在C#中有这个WebMethod:C#WebMethod未返回JSON对象,c#,jsonp,jquery-select2,webmethod,C#,Jsonp,Jquery Select2,Webmethod,我在C#中有这个WebMethod: [WebMethod] [System.Web.Script.Services.ScriptMethod(UseHttpGet=true,ResponseFormat=System.Web.Script.Services.ResponseFormat.Json)] [System.Web.Http.Cors.EnableCors(“*”,标题:“*”,方法:“*”)] 公共名单获得者特别许可证() { 列表特殊数据=新列表(); 新增(新DatoMaestr
[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet=true,ResponseFormat=System.Web.Script.Services.ResponseFormat.Json)]
[System.Web.Http.Cors.EnableCors(“*”,标题:“*”,方法:“*”)]
公共名单获得者特别许可证()
{
列表特殊数据=新列表();
新增(新DatoMaestro(1,“皮肤病”);
添加(新DatoMaestro(2,“Medicina General”);
返回特殊数据;
//var oSerializer=new System.Web.Script.Serialization.JavaScriptSerializer();
//返回oSerializer.Serialize(o);
}
当我调用它时,返回一个xml,即使我指定必须返回Json。当我使用JavaScriptSerializer时,JSON返回到to中,因此它是一个有效的XML文档
我试图通过调用该WebMethod来填充select2控件
这是客户端代码:
$('#Especialidad').select2({
allowClear: true,
width: '80%',
placeholder: 'Seleccione la especialidad',
minimumResultsForSearch: -1,
ajax: {
url: "http://localhost:32617/Maestro.asmx/ObtieneEspecialidades",
dataType: 'jsonp',
type: 'GET',
results: function(data, page) {
alert(data);
var mas = (page * 10) < data.total; // whether or not there are more results available
// notice we return the value of more so Select2 knows if more results can be loaded
return {results: data.especialidades, more: mas};
}
}
});
$('specialidad')。选择2({
allowClear:是的,
宽度:“80%”,
占位符:“Seleccione la Specialidad”,
研究的最小结果:-1,
阿贾克斯:{
url:“http://localhost:32617/Maestro.asmx/ObtieneEspecialidades",
数据类型:“jsonp”,
键入:“GET”,
结果:功能(数据、页面){
警报(数据);
var mas=(第*10页)
当我调用它时,我收到了错误:
Uncaught SyntaxError: Unexpected token <
未捕获的语法错误:意外标记<
这是显而易见的,因为标记显示为第一个元素。在Javascript调用中使用“POST”而不是“GET”: 发件人: 确保请求是POST请求,而不是GET请求 另外,请查看以下内容:
调用是否正常工作并返回一些内容?
Uncaught SyntaxError: Unexpected token <