Home > Backend Development > PHP Tutorial > ajax POST json对象给PHP,PHP怎么接收值

ajax POST json对象给PHP,PHP怎么接收值

WBOY
Release: 2016-06-13 12:01:29
Original
1306 people have browsed it

ajax POST json对象给PHP,PHP如何接收值

本帖最后由 KtosMayCry 于 2014-07-04 20:30:03 编辑 前端:
<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 />
Copy after login

PHP:
<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 />
Copy after login

通过 JSON.stringify 获取到的json格式如下:
<br /> [{ "name":"Bill" , "value":"Gates" },<br />{ "name":"George" , "value":"Bush" },<br />{ "name":"Thomas" , "value":"Carter" }]<br />
Copy after login

PHP,这里POST的接收值不知道用什么,如果把上面的json字符串改成如下格式,那么PHP即可接受到并直接返回数组格式
<br /> {"data":[{ "name":"Bill" , "value":"Gates" },<br />{ "name":"George" , "value":"Bush" },<br />{ "name":"Thomas" , "value":"Carter" }]}<br />
Copy after login

总结下主要想问两个问题:

1、JQuery如何把serializeArray()获取到的json对象转换为 第二种格式的json字符串 且保证中文不会乱码

2、或者php如何直接接收 json对象 或者 第一种格式的 json字符串
------解决方案--------------------
1、JQuery如何把serializeArray()获取到的json对象转换为 第二种格式的json字符串 且保证中文不会乱码

保证不乱码,需要在header加入


第二种数组格式
var fields = $("#rss_form").serializeArray();
var t = {};
t['data'] = fields
fields = JSON.stringify(t);

2、或者php如何直接接收 json对象 或者 第一种格式的 json字符串
用第一种json字符串提交例子:
<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 />
Copy after login

demo.php
<br /><?php<br />$data = $_POST['data'];<br />file_put_contents('test.log', $data, true);<br />echo "ok";<br />?><br />
Copy after login


运行后:
test.log内容为[{"name":"中文","value":"中文"},{"name":"中文","value":"中文"},{"name":"中文","value":"中文"}]
如果没有加 则会乱码。
------解决方案--------------------
1、ajax 本身就是以 utf-8 编码传输,所以不需要再有 charset=utf-8 声明
2、jq 的 post 方式已经发了 application/x-www-form-urlencoded 头,也不需要你再劳动了
所以
      $.ajaxSetup({<br />        cache:false,<br />        contentType : "application/x-www-form-urlencoded; charset=utf-8"<br />      });<br />
Copy after login
一段是不需要的。考虑的 IE 的缓存可能会影响效果,可以把 cache: false 作为 $.ajax 的参数
      $.ajax({<br>        cache: false,<br>        type: "POST",<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template