zoukankan      html  css  js  c++  java
  • jquery 调用ajax返回json

    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);
    }
    });
    }

  • 相关阅读:
    jeecg中树形显示的用法
    jeecg中ajax传值的前端js和后台代码
    js中去掉字符串中的某个指定字符
    Address already in use: JVM_Bind(端口冲突)
    jeecg中List页面标签的用法
    HTML map标签图片区域链接
    CSS块级元素和行内元素
    discuz 标签
    JavaScript中getYear()各个浏览器的差异
    [HNOI2017] 礼物
  • 原文地址:https://www.cnblogs.com/brantshi/p/4061666.html
Copyright © 2011-2022 走看看