今天看了下jQquery中的getJSON()方法,做点小结:

原型:

jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] )

This is a shorthand Ajax function, which is equivalent to

官网解释:意思是$.getJSON是Ajax方法的简写,等价于下面的写法:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
由于我刚学jQuery,所以下面的写法也不甚理解。没关系,这不影响我照葫芦画瓢。
上面可以看出,url和seccess function是必须的,[data]是可选的,刚入门,就来个简单的。
首先新建一个ajax.php文件
<html>
<head>
<!-- 引入jQuery -->
<script src="jquery-1.10.1.js" type="text/javascript"></script>
<script type="text/javascript"> $(document).ready(function() {
$("#sent").click(function() {
$.getJSON('ajax_json.php', function(data){
$.each(data, function(key, value) {
$("#get_content").append(key + ':' + value + '<br/>');
})
});
});
}); </script>
</head>
<body>
<button id="sent">Ajax</button>
<div id="get_content"></div>
</body>
</html>

上面的文件是获取 ajax_json.php 文件中的json数据, ajax_json.php很简单,主要是json数据(这里顺便学习了json_encoe()函数)如下:

<?php
$arr = array('name' => 'hui314', 'sex' => 'man');
echo json_encode($arr);

当点击id=“sent”这个button,id=“get_content”的div就会显示得到的json数据:

name:chen
sex:man

很简单:)

 

jQuery.getJSON()方法小记的更多相关文章

  1. Jquery getJSON方法分析

    准备工作 ·Customer类 public class Customer {     public int Unid { get; set; }     public string Customer ...

  2. jquery之getJSON方法获取中文数据乱码解决方法

    最近公司做的东西要用到js,感觉js太繁琐,所以自己学起了jquery,发现jquery确实强大.在学到jquery ajax的时候(用的工具是eclipse),发现$.getJSON()方法请求服务 ...

  3. JQuery 获取json数据$.getJSON方法的实例代码

    这篇文章介绍了JQuery 获取json数据$.getJSON方法的实例代码,有需要的朋友可以参考一下 前台: function SelectProject() { var a = new Array ...

  4. jQuery ajax - getScript() 方法和getJSON方法

    实例 使用 AJAX 请求来获得 JSON 数据,并输出结果: $("button").click(function(){ $.getJSON("demo_ajax_js ...

  5. jQuery的get()post()getJson()方法

    jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据. HTTP 请求:GET vs. POST 两种在客户端和服务器端进行请求-响应的常用方 ...

  6. jQuery中ajax请求的六种方法(三、四):$.getJSON()方法

    4.$.getJSON()方法 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  7. jQuery工具方法

    目录 常用工具方法 判断数据类型的方法 Ajax操作 $.ajax 简便写法 Ajax事件 返回值 JSONP 文件上传 参考链接 jQuery函数库提供了一个jQuery对象(简写为$),这个对象本 ...

  8. $.getJSON()方法的 callback说明

    $.getJSON()方法跨域 去取得服务器的json对象的时候,url的后缀最后带一个"callback=?"的参数作为成功的回调函数:如: var url = "${ ...

  9. JSONP跨域原理和jQuery.getJSON用法

    JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式).本文主要介绍JS ...

随机推荐

  1. CentOS7.x 通过mail命令发,使用465端口(smtps协议)发送邮件

    #创建证书mkdir -p /root/.certs/echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CE ...

  2. nagios系列教程地址

    http://www.sosidc.com/sort/10/page/3 http://www.sosidc.com/sort/10/page/2 http://www.sosidc.com/sort ...

  3. leetcode268:Missing Number

    描写叙述 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is mis ...

  4. 判断一个字符串是否为合法IP

    输入任意一个字符串,判断是否为合法IP bool IsIPAddress(const char * str){ //先判断形式是否合法, //检查是否只包含点和数字 ; str[i] != '\0'; ...

  5. 用BSF + Beanshell使Java程序能够运行字符串形式的代码(转载)

    BSF(Bean Scripting Framework)最初是IBM Alpha工作组的项目,后来贡献给了Apache,BSF使Java能够更好的与各种流行脚本语言整合,实现不同语言之间的交互功能. ...

  6. React Native :加载新闻列表

    代码地址如下:http://www.demodashi.com/demo/13212.html 标签与内容页联动 上一节(React Native : 自定义视图)做到了点击标签自动移动,还差跟下面的 ...

  7. 檢查php文件中是否含有bom的php文件

    原文链接: http://www.cnblogs.com/Athrun/archive/2010/05/27/1745464.html 另一篇文章:<关于bom.php>,http://h ...

  8. php文件加载路径

    <?php require('reusable.php'); echo "相对路径加载<br/>"; /* ./ 表示当前文件所在的目录 ../ 表示当前文件所在 ...

  9. 我也来谈谈使用Zen Coding快速开发html和css原理

    zen coding 是一种仿css选择器的语法来快速开发html和css的开源项目.现已更名为Emmet.可以到github上下载拜读.在这个都想偷懒的世界里,此方法可以极大的缩短开发人员的开发时间 ...

  10. json.Decoder vs json.Unmarshal

    128down voteaccepted It really depends on what your input is. If you look at the implementation of t ...