ie调用可以,火狐和chrome皆失败,找了半天原因。
被屏蔽了。
火狐和chrome 对同一个域名不同端口的调用也严格限制,不给调用。只能用jsonp。
查看网络的返回状态,错误信息,F12 很重要。
ashx代码:
if (context.Request["callback"] != null)
{
callbackName = context.Request["callback"];
callbackName = callbackName.Split(',')[0];
}
context.Response.Clear();
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
var predicateWhere = PredicateBuilder2.True<SYS_Organization>();
predicateWhere = predicateWhere.And(t => t.Status == 0);
ISYSOrganizaService service = new SYSOrganizaService();
List<Organ> list = service.GetList<SYS_Organization>(predicateWhere).Select(t => new Organ { ID = t.ID, Name = t.Name }).ToList();
HttpContext.Current.Response.Write(callbackName + "([");
int i = 0;
foreach (Organ organ in list)
{
if(i>0)
HttpContext.Current.Response.Write(",");
HttpContext.Current.Response.Write("{"Name":"" + organ.Name + "","ID":"" + organ.ID.ToString() + ""}");
i++;
}
HttpContext.Current.Response.Write("]);");
context.Response.Flush();
context.Response.End();
js代码:
function GetJsonpData(callback,data,url) {
$.ajax({
async: false,
crossDomain: true,
type: "GET",
dataType: "jsonp",
jsonpCallback: callback,
//jsonp: "callback", //默认callback
url: url,
data: data,
contentType: "application/json; charset=utf-8",
success: function (json) { },
beforeSend: function () { },
complete: function (data, status) { },
error: function (XMLHttpRequest, textStatus, errorThrown) {
var t1 = textStatus;
var t2 = XMLHttpRequest.status;
var t3 = XMLHttpRequest.readyState; ;
var t4 = XMLHttpRequest.responseText;
// alert(textStatus);
}
});
}