ajax POST json对象给PHP,PHP如何接收值
<br /> $("#save_config_btn").click(function(){<br /> <br /> $.ajaxSetup({<br /> cache:false,<br /> contentType : "application/x-www-form-urlencoded; charset=utf-8"<br /> });<br /> <br /> var fields = $("#rss_form").serializeArray();<br /> <br /> fields = JSON.stringify(fields) //中文会乱码<br /> <br /> $.ajax({<br /> type: "POST",<br /> url : "edit.php",<br /> data: fields,<br /> success: function(msg){<br /> alert(msg);<br /> }<br /> });<br /> <br /> });<br />
<br /><?php<br />header("Content-type: text/html; charset=UTF-8");<br /><br />$data = $_POST['data'];<br /><br />echo '<pre class="brush:php;toolbar:false">';<br />print_r($data);<br /><br />?><br />
<br /> [{ "name":"Bill" , "value":"Gates" },<br />{ "name":"George" , "value":"Bush" },<br />{ "name":"Thomas" , "value":"Carter" }]<br />
<br /> {"data":[{ "name":"Bill" , "value":"Gates" },<br />{ "name":"George" , "value":"Bush" },<br />{ "name":"Thomas" , "value":"Carter" }]}<br />
<br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /> <head><br /> <meta http-equiv="content-type" content="text/html;charset=utf-8"><br /> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script><br /> <title> js test </title><br /> </head><br /><br /> <body><br /> <script type="text/javascript"><br /> var fields = [{ "name":"中文" , "value":"中文" },{ "name":"中文" , "value":"中文" },{ "name":"中文" , "value":"中文" }];<br /> fields = "data=" + JSON.stringify(fields);<br /> $.ajax({<br /> type: "POST",<br /> url : "demo.php",<br /> data: fields,<br /> success: function(msg){<br /> alert(msg);<br /> }<br /> });<br /> </script><br /><br /> </body><br /></html><br />
<br /><?php<br />$data = $_POST['data'];<br />file_put_contents('test.log', $data, true);<br />echo "ok";<br />?><br />
$.ajaxSetup({<br /> cache:false,<br /> contentType : "application/x-www-form-urlencoded; charset=utf-8"<br /> });<br />一段是不需要的。考虑的 IE 的缓存可能会影响效果,可以把 cache: false 作为 $.ajax 的参数
$.ajax({<br> cache: false,<br> type: "POST",<div class="clear"> </div>