您的位置:首页 > Web前端 > JQuery

php+json+ajax+jquery小例子

2016-07-04 11:53 736 查看
使用Jquery中的ajax方法传递JSON数据包,传递的方法使用POST。

初次接触,例子比较简单。

写了两个文件:test.html、test.php

test.html:

<html>

<head>

 $("#send").click(function(){

              var con = $("input").serialize();

             $.ajax({

                         url:"test.php",

                         type:"post",

                         dataType:"json",

                         data:con

                         success:function(data){

                                 var str = data.username + data.age + data.address;

                                alert(str);

                         }

            });

});

</head>

<body>

<form id="send" method=""post">

<p><span>姓名:</span><input type="text" name="username"></p>

<p><span>年龄:</span><input type="text" name="age"></p>

<p><span>住址:</span><input type="text" name="address"></p>

</form>

</body>

</html>

test.php:

<?php

header("content-type:text/html;charset:utf-8");

$json_arr['username'] = $_POST['username'];

$json_arr['age'] = $_POST['age'];

$json_arr['job'] = $_POST['job'];

echo json_encode($json_arr);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax post json php j