jQuerygetJSON處理json數(shù)據(jù)的代碼解決ajax跨域返回json數(shù)據(jù)問(wèn)題

本人遇到的問(wèn)題 :
    用getJSON 異步調(diào)用數(shù)據(jù),返回json 數(shù)據(jù)集合,數(shù)據(jù)是反回了 但是 沒(méi)有執(zhí)行回調(diào)函數(shù)


正確后的代碼:
js文件
//獲取列表
function getlist() {
    var topID = $("#articleid").val();
    //var params = "topicID=" + topID;

    $.getJSON("http://news." + domian + "/AjaxList.ashx?topicID="+topID+"&format=json&jsoncallback=?", function (data) {
        $("#commentlist").html(data.contentlist);

    });
}

一般處理文件
<%@ WebHandler Language="C#" Class="AjaxList" %>

using System;
using System.Web;
using LitJson;
using UCenter.Logic.Members;
using WCms.Entity.Common;
using WCms.Logic.Common;
using Qyn.Studio.Extend;
using Qyn.Studio.Utils;
/// <summary>
/// 獲取電子優(yōu)惠卷
/// </summary>
public class AjaxList : IHttpHandler
{

    public void Proce***equest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("P3P", "CP=CAO PSA OUR");
       
        var data = new JsonData();
        var contentlist = new StringPlus();
        //var ID=UserLogic.GetCookies().ID;
        //var userinfo = UserLogic.Data.Where(t => t.ID == ID.GetValueOrDefault()).ToInfo();
        var jsoncallback = QynRequest.QS("jsoncallback");
        var cTopicID = QynRequest.QS("topicID").ConvertType(0);

        if (cTopicID > 0)
        {
            var list = WCms.Logic.Common.CommentLogic.Data.Where(t => t.TopicID == cTopicID.ConvertType(0)).Selector(t => new { t.Contents, t.CreateAt, t.CreateName })
               .Desc(t => t.CreateAt).ToList(5);

            contentlist.AppendLine("<div class=\"reading\">");
            contentlist.AppendLine("<h4><span>網(wǎng)友評(píng)論</span></h4>");
            contentlist.AppendLine("<div class=\"Comment\">");
            foreach (var item in list)
            {
                contentlist.AppendLine(string.Format("<dl><dt><img src=\"/content/imges/small.jpg\" alt=\"{0}\"/></dt>", item.CreateName));
                contentlist.AppendLine(string.Format("<dd><p><i>{0}:</i>{1}</p>", item.CreateName, item.Contents));
                contentlist.AppendLine(string.Format("<p><span>時(shí)間:{0}</span></p>", item.CreateAt.ToShortString("yyyy-MM-dd hh:mm")));
                contentlist.AppendLine("</dd></dl>");
            }
            contentlist.AppendLine("</div></div>");
            data["contentlist"] = contentlist.Value;
        }
        context.Response.Write(jsoncallback + "(" + data.ToJson() + ")");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}







劃紅線(xiàn)的部分是重點(diǎn)需要注意的事情

注:format=json  可以不加
    jsoncallback   也可以換成  callback
    不過(guò)一般處理文件里面 也必須要和 js文件里的 jsoncallback 或著 callback  一致。


以上就是這個(gè)問(wèn)題的解決辦法 ,如有問(wèn)題請(qǐng)留言

網(wǎng)站欄目:jQuerygetJSON處理json數(shù)據(jù)的代碼解決ajax跨域返回json數(shù)據(jù)問(wèn)題
文章來(lái)源:http://muchs.cn/article30/gdsjpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、域名注冊(cè)、用戶(hù)體驗(yàn)、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)公司網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名