jquery中$.getJSON 的使用方法

jQuery.getJSON(url, [data], [callback])

通过 HTTP GET 请求载入 JSON 数据。

参数:

url,[data],[callback]                          String,Map,FunctionV1.0

url : 发送请求地址。

data : 待发送 Key/value 参数。

callback : 载入成功时回调函数。

getJson的使用方法 jQuery.getJSON(url,[data],[callback])
要获得一个json文件的内容,就可以使用$.getJSON()方法,这个方法会在取得相应文件后对文件进行处理,并将处理得到的JavaScript对象提供给代码.
回调函数提供了一种等候数据返回的方式,而不是立即执行代码,回调函数也需要一个参数,该参数中保存着返回的数据。这样我们就可能使用jQuery提供的另一个全局函数(类方法).each()来实现循环操作,将.getJSON函数返回的每组数据循环处理。

首先创建一个JSONHelper.css类在创建两方法一个用来返回getJSON的返回值,另外一个用来序列化对象·····

复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
//序列
using System.Web.Script.Serialization;
namespace SCJob.WebUI.JsonHelper
{
    public class JSONHelper : System.Web.UI.Page
    {
        
        /// <summary>
        /// 返回JSON数据到请求的函数
        /// </summary>
        /// <param name="callback">回调参数</param>
        /// <param name="data">序列的JSON字符串</param>
        /// <param name="Response">与Page对象关联的HttpResponse对象</param>
        public void ReturnJSON(string callback, string data, HttpResponse Response)
        {
            string result = string.Format("{0}({1})", callback, data);
            Response.Expires = -1;
            Response.Clear();
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType = "application/json";
            Response.Write(result);
            Response.Flush();
            Response.End();
        }
        /// <summary>
        /// 转换为JSON数据
        /// </summary>
        /// <param name="obj">要转换为JSON数据的对象</param>
        /// <returns>返回JSON字符串</returns>
        public string ToJSON(object obj)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }
    }
}

复制代码

复制代码

$('#btnAllrefresh').click(function () {
    $.getJSON('JobList.aspx?jsoncallback=?&operate=allRefresh', function (data) {
        if (data.error) {
            $.message(data.error, { title: '系统提示', icon: 'fail' });
            //这里的$.message方法是自己写的函数用来做用户体验的  
            //自己可以用alert方法data.error获得后台代码的返回值error为返回的json串中的键           
            return;
        }
        if (data.message) {
            $.message(data.message, { title: '操作提示', icon: 'fail' });
        }
    })
});

复制代码

上面的代码和下面的这两段代码都是前台的jquery代码按钮的点击事件
 

复制代码

$.getJSON("JobList.aspx?jsoncallback=?", { operate: 'deleteJob', jobID: jobID }, function(data) {
    if (data && data.error) {
        $.message(data.error, { title: '系统提示', icon: 'fail' });
        return;
    }
    $.anchorMsg('删除职位成功');
    $('#row' + jobID).next('.getlist_line').andSelf().remove();
})

复制代码

复制代码

 private JSONHelper jsonHelper = new JSONHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["operate"] != null)
            {
                string operate = Request.QueryString["operate"];
                //删除职位信息
                if (operate == "deleteJob")
                {
                    if (Request.QueryString["jobID"] != null)
                    {
                        DeleteJob(int.Parse(Request.QueryString["jobID"]));
                    }
                }
            }
        }
        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="JobId">职位编号</param>
        public void DeleteJob(int JobId)
        {
            SC_CpApplyJob job = new SC_CpApplyJob();
            job = jobs.GetEntity(JobId);
            string message = "{\"succee\":\"OK\"}";
            try
            {
                //执行删除操作
                jobs.DelEmpty(job);
            }
            catch (Exception ex)
            {
                //自己写的json字符串用来作为返回值 方便前台使用
                message = "{\"error\":\"删除失败\"}";
                throw;
            }
            finally
            {
                //调用自己创建的类中的方法  参数说明:Response当前的HttpResponse对象主要用来输出到前台中的数据有用
                jsonHelper.ReturnJSON(Request.QueryString["jsoncallback"], message, Response);
            }
        }

复制代码

猜你喜欢

转载自blog.csdn.net/Andrewniu/article/details/81664197