jquery post data中data和status的数据是怎么来的

jquery中$.post()方法的简单实例
字体:[ ] 类型:转载 时间:
本篇文章主要是对jquery中$.post()方法的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
在jqery中有这样一个方法,$.post()下面就这个方法做一个简单的实例:
jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求
url (String) : 发送请求的URL地址.
data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。
callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)
1.html页面(index.html) 代码如下:&!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&&html xmlns="http://www.w3.org/1999/xhtml"&&head&&meta http-equiv="Content-Type" content="text/ charset=gb2312" /&&title&Untitled Document&/title&&script type="text/javascript" src=\'#\'" /jquery-1.3.2.js"&&/script&&script language="javascript"&function checkemail(){& if($('#email').val() == ""){&&& $('#msg').html("please enter the email!");&&& $('#email').&&&& }& if($('#address').val() == ""){&&& $('#msg').html("please enter the address!");&&& $('#address').&&&& }& ajax_post();}
function ajax_post(){& $.post("action.php",{email:$('#email').val(),address:$('#address').val()},& function(data){&&& //$('#msg').html("please enter the email!");&&& //alert(data);&&& $('#msg').html(data);& },& "text");//这里返回的类型有:json,html,xml,text}&/script&&/head&
&body&&form id="ajaxform" name="ajaxform" method="post" action="action.php"&&&& &p&&&& email&input type="text" name="email" id="email"/&&&& &/p&&&& &p&&&& address&input type="text" name="address" id="address"/&&&& &/p&&&& &p id="msg"&&/p&&&& &p&&&& &&&&&&& &input name="Submit" type="button" value="submit" onclick="return checkemail()"/&&&& &/p&&/form&&/body&&/html&2.php页面(action.php) 代码如下:&?php$email = $_POST["email"];$address = $_POST["address"];
//echo $//echo $echo "success";?&说明:当点击按钮时,注意按钮现在的类型是button.在不使用$.post()方法时,按钮类型是submit,这样submit提交form里的数据,采用post方法传递到页面action.php,这时在页面action.php中就能接受到传过来的数据。当采用$.post方法时,我们在函数ajax_post()方法中其实就是使用了post的方法。(要引用jquery库文件)
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具  使用jquery.post获取json数据,并对返回的json数据进行处理。php相应post请求返回json数据,js将返回数据转换为json对象进行处理。
  前端:使用jquery.post请求数据
$.post(url,data,success(data))
  响应请求:使用php响应请求(前面借鉴了phpwind的响应ajax请求,所以返回的是xml格式数据)
$arr = array ('a'=&1,'b'=&2,'c'=&3,'d'=&4,'e'=&5);
$output = json_encode($arr);
//{"a":1,"b":2,"c":3,"d":4,"e":5}
header("Content-Type: text/charset=utf-8");
echo "&?xml version=\"1.0\" encoding=\"utf-8\"?&&ajax&&![CDATA[" . $output . "]]&&/ajax&";
  前端处理返回数据:首先取出xml中数节点值,然后将其转换为json对象
function success(result){
if(typeof(result) == "object"){
result = result.lastChild.firstChild.nodeV//xml取节点值
eval("result = "+result);//转换为json对象
相关知识点:
  PHP中的JSON&
  1、json_encode():该函数主要用来将数组和对象,转换为json格式
$arr = array ('a'=&1,'b'=&2,'c'=&3,'d'=&4,'e'=&5);
echo json_encode($arr);
//{"a":1,"b":2,"c":3,"d":4,"e":5}
$obj-&body = 'another post';
$obj-&id = 21;
$obj-&approved = true;
$obj-&favorite_count = 1;
$obj-&status = NULL;
echo json_encode($obj);
$obj-&body = 'another post';
$obj-&id = 21;
$obj-&approved =
$obj-&favorite_count = 1;
$obj-&status = NULL;
echo json_encode($obj);
  注:json只能是utf-8编码,son_encode()的参数必须是utf-8编码,否则会得到空字符或者null
  2、索引数组和关联数组
  索引数组
$arr = Array('one', 'two', 'three');
echo json_encode($arr);
//"[]"(数组)
//["one","two","three"]
  关联数组
$arr = Array('1'=&'one', '2'=&'two', '3'=&'three');
echo json_encode($arr);
//"{}"(对象)
//{"1":"one","2":"two","3":"three"}
  将"索引数组"强制转化成"对象"
json_encode( (object)$arr );
//json_encode ( $arr, JSON_FORCE_OBJECT );
  3、类(class)的转换  公开变量(public),其他东西(常量、私有变量、方法等等)都遗失
class Foo {
    const ERROR_CODE = '404';
    public $public_ex = 'this is public';
    private $private_ex = 'this is private!';
    protected $protected_ex = 'this should be protected';
    public function getErrorCode() {
      return self::ERROR_CODE;
$foo = new F
$foo_json = json_encode($foo);
echo $foo_json;
//{"public_ex":"this is public"}
  4、json_decode()  将json文本转换为相应的PHP数据结构  json_decode()总是返回一个PHP对象  如果想要强制生成PHP关联数组,json_decode()需要加一个参数true
$json = '{"foo": 12345}';
$obj = json_decode($json);
print $obj-&{'foo'};
  5、注意  1)、json只能用来表示对象(object)和数组(array),如果对一个字符串或数值使用json_decode(),将会返回null  2)、json的分隔符(delimiter)只允许使用双引号,不能使用单引号  3)、json名值对的"名"(冒号左边的部分),任何情况下都必须使用双引号  4)、最后一个值之后不能添加逗号(trailing comma)
  &处理返回json数据
  将返回数据转换为json,私用eval(),但报"invalid label"错误,解决方法
var json = eval('(' + response + ')');
//或 eval('var json = ' + response);
  [1]   [2]
阅读(...) 评论()<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&jquery post中data和status的数据是怎么来的_百度知道首先认识要jQuery.post(url, [data], [callback], [type])对参数进行说明:url:发送请求地址。data:待发送 Key/value 参数。callback:发送成功时回调函数。type:返回内容格式,xml, html, script, json, text, _default。首先建立php实例:&?php
echo json_encode(array("name"=&$_POST['name']));
?&然后建立ajax.html文件,注意js代码:&html&
&meta http-equiv="Content-Type" content="text/ charset=UTF-8"&
&title&php点点通 - 关注php开发,提供专业web开发教程! &/title&
&script type="text/javascript" src="./jquery-1.7.1.min.js"&&/script&
$(document).ready(function(){
$("#sub").click(function(){
$.post("testPost.php",{name:$("#name").val()},function(data,textStatus){
$("#result").append("data:"+data.name);
$("#result").append("&br&textStatus:"+textStatus);
},"json");
&form action="testPost.php" method="post"&
&input type="text" name="name" id="name" &
&input type="submit" id="sub" value="提交"&
&h2&显示的内容如下:&/h2&
&div id="result"&&/div&
&/html&以上js代码的初始界面是:输入文字提交后可以看到:用FireBug可以看到传输的数据类型:说明:json是一种轻量级的数据格式,json_encode & 对编码进行JSON编码!
关注微信公众平台

我要回帖

更多关于 jquery post提交数据 的文章

 

随机推荐