使用json_decode返回PHP null

时间:2015-06-02 00:07:53

标签: php json

由于某种原因,我遇到了由WYSIWYG编辑器生成HTML的json字段的问题。

我正在使用wordpress和angularJS,我正在尝试创建一个更新某个帖子的表单。

获取数据正常工作,例如我在服务器上做的事情

$data = [
'title'=>$post->post_title,
'description'=>$post->post_content // contains html via a WYSIWYG editor
.... more stuff
]
header('Content-Type: application/json');
echo json_encode($data);
exit;

表格自动填充以上数据确定。

当我尝试通过将JSON.stringify(数据)返回给服务器来更新帖子时,问题就出现了。当我在服务器上尝试json_decode($ _ POST ['data'])时,由于data.description的HTML内容,我得到以下PHP错误。没有data.description我的代码可以正常更新帖子

json_last_error: 4
json_last_error_msg: "Syntax error"
json_decode($_POST['data']) == null 

但在Javascript中,当我执行console.log(JSON.stringify(data))并验证JSON数据时没问题。

我没有启用魔术配额get_magic_quotes_gpc()=> 0。 我也尝试过striplashes($ _ POST ['data']);

这个问题看起来很奇怪,因为数据已成功通过php转换为json,然后通过javascript解码,但反向操作无效。

$ _ POST ['data'] value ..在服务器接收时,JSON字符串不完整

{\"deadline\":\"31 Août 2015\",\"country\":\"Canada\",\"city\":\"Montréal-Est\",\"province\":\"Québec\",\"jobillicoCategory\":\"180\",\"recruiterName\":\"HR Team\",\"recruiterEmail\":\"hr@workland.ca\",\"titleEn\":\"\",\"descriptionEn\":\"\",\"requestId\":\"d4618389-6bb5-5e51-8489-d39a4fc09016\",\"ID\":1996,\"title\":\"Buyer - Planner\",\"description\":\"<p><span lang=\\\"en-US\\\">We are currently looking for an experienced, </span><span lang=\\\"en-US\\\">motivated and ready</span><span lang=\\\"en-US\\\"> buyer-planner, </span><span lang=\\\"en-US\\\">t</span><span lang=\\\"en-US\\\">o take on new challenges. The </span><span lang=\\\"en-US\\\">candidate\'s</span><span lang=\\\"en-US\\\"> responsibility </span><span lang=\\\"en-US\\\">is </span><span lang=\\\"en-US\\\">to fulfill and coordinate production planning activities within </span><span lang=\\\"en-US\\\">customer </span><span lang=\\\"en-US\\\">constraints and </span><span lang=\\\"en-US\\\">re</span><span lang=\\\"en-US\\\">quirements </span><span lang=\\\"en-US\\\">in order </span><span lang=\\\"en-US\\\">to ensure </span><span lang=\\\"en-US\\\">customer </span><span lang=\\\"en-US\\\">satisfaction and the achievement of business objectives. In addition, </span><span lang=\\\"en-US\\\">they</span><span lang=\\\"en-US\\\"> will </span><span lang=\\\"en-US\\\">carry out</span><span lang=\\\"en-US\\\"> the p</span><span lang=\\\"en-US\\\">urchasing </span><span lang=\\\"en-US\\\">of components necessary for production, </span><span lang=\\\"en-US\\\">while </span><span lang=\\\"en-US\\\">respecting GMP </span><span lang=\\\"en-US\\\">regulations</span><span lang=\\\"en-US\\\"> and purchasing polic</span><span lang=\\\"en-US\\\">ies</span><span lang=\\\"en-US\\\">. </span></p>\\r\\n<p>

console.log(JSON.stringify(data))的值是完整的

{"deadline":"31 Août 2015","country":"Canada","city":"Montréal-Est","province":"Québec","jobillicoCategory":"180","recruiterName":"HR Team","recruiterEmail":"hr@workland.ca","titleEn":"","descriptionEn":"","requestId":"d4618389-6bb5-5e51-8489-d39a4fc09016","ID":1996,"title":"Buyer - Planner","description":"<p><span lang=\"en-US\">We are currently looking for an experienced, </span><span lang=\"en-US\">motivated and ready</span><span lang=\"en-US\"> buyer-planner, </span><span lang=\"en-US\">t</span><span lang=\"en-US\">o take on new challenges. The </span><span lang=\"en-US\">candidate's</span><span lang=\"en-US\"> responsibility </span><span lang=\"en-US\">is </span><span lang=\"en-US\">to fulfill and coordinate production planning activities within </span><span lang=\"en-US\">customer </span><span lang=\"en-US\">constraints and </span><span lang=\"en-US\">re</span><span lang=\"en-US\">quirements </span><span lang=\"en-US\">in order </span><span lang=\"en-US\">to ensure </span><span lang=\"en-US\">customer </span><span lang=\"en-US\">satisfaction and the achievement of business objectives. In addition, </span><span lang=\"en-US\">they</span><span lang=\"en-US\"> will </span><span lang=\"en-US\">carry out</span><span lang=\"en-US\"> the p</span><span lang=\"en-US\">urchasing </span><span lang=\"en-US\">of components necessary for production, </span><span lang=\"en-US\">while </span><span lang=\"en-US\">respecting GMP </span><span lang=\"en-US\">regulations</span><span lang=\"en-US\"> and purchasing polic</span><span lang=\"en-US\">ies</span><span lang=\"en-US\">. </span></p>\r\n<p>&nbsp;</p>\r\n<p><strong>Main Responsibilities </strong></p>\r\n<ul>\r\n<li>\r\n<p><a name=\"result_box2\"></a><span lang=\"en-US\">Carry out product </span><span lang=\"en-US\">orders</span><span lang=\"en-US\"> from suppliers;</span>;</p>\r\n</li>\r\n<li>\r\n<p><a name=\"result_box3\"></a><span lang=\"en-US\">Participate in the research, selection, evaluation and validation of suppliers</span>;</p>\r\n</li>\r\n<li>\r\n<p><span lang=\"en-US\">S</span><span lang=\"en-US\">chedul</span><span lang=\"en-US\">e</span><span lang=\"en-US\"> orders </span><span lang=\"en-US\">according to </span><span lang=\"en-US\">clients</span><span lang=\"en-US\">'</span> <span lang=\"en-US\">needs</span><span lang=\"en-US\"> and business objectives</span>;</p>\r\n</li>\r\n<li>\r\n<p><span lang=\"en-US\">Research quotes</span><span lang=\"en-US\"> and </span><span lang=\"en-US\">collect </span><span lang=\"en-US\">samples for submissions</span>;</p>\r\n</li>\r\n<li>\r\n<p><a name=\"result_box6\"></a><span lang=\"en-US\">Perform other duties as required by the company. </span></p>\r\n</li>\r\n</ul>\r\n<p><strong><br />Requirements</strong></p>\r\n<ul>\r\n<li>\r\n<p><span lang=\"en-US\">BA</span><span lang=\"en-US\">C</span><span lang=\"en-US\"> in Operations Management (GOP), Business Administration </span><span lang=\"en-US\">in L</span><span lang=\"en-US\">ogistic</span><span lang=\"en-US\">s</span><span lang=\"en-US\"> or other</span>;</p>\r\n</li>\r\n<li>\r\n<p><a name=\"result_box8\"></a> <span lang=\"en-US\">B</span><span lang=\"en-US\">etween one and three years </span><span lang=\"en-US\">of </span><span lang=\"en-US\">experience in a manufacturing company</span>;</p>\r\n</li>\r\n<li>\r\n<p>Advanced skills in Microsoft Office (Word, Excel);</p>\r\n</li>\r\n<li>\r\n<p>Good knowledge of ERP systems.</p>\r\n</li>\r\n</ul>\r\n<p><strong><br />Skills</strong></p>\r\n<ul>\r\n<li>\r\n<p><a name=\"result_box9\"></a> <span lang=\"en-US\">Knowledge in the order </span><span lang=\"en-US\">of </span><span lang=\"en-US\">production and management of raw materials / components</span>;</p>\r\n</li>\r\n<li>\r\n<p><a name=\"result_box10\"></a> <span lang=\"en-US\">Professionalism, interpersonal skills and strong ability in work organization;</span></p>\r\n</li>\r\n<li>\r\n<p><a name=\"result_box12\"></a> <span lang=\"en-US\">Resourcefulness and initiative, active listening and understanding customer needs</span>;</p>\r\n</li>\r\n<li>\r\n<p><a name=\"result_box13\"></a> <span lang=\"en-US\">Good analytical skills, concern for improvement, team spirit, good communication skills and leadership;</span></p>\r\n</li>\r\n<li>\r\n<p>Good negotiation skills.</p>\r\n</li>\r\n</ul>\r\n<p><a name=\"result_box14\"></a> <span lang=\"en-US\"><br />Join a growing dynamic company located in eastern Montreal. The company offers benefits, continuing education, a dynamic working environment and opportunities for advancement. </span></p>"}

更新 好吧,我认为这是'&amp;';在将json发送到服务器时打破json的HTML。如何消毒? encodeURIComponent()?或者替换()?

 function ($http, globals) {
     var request = {
         method: 'POST',
         url: globals.ajax_url,
         headers: {
             'Content-Type': 'application/x-www-form-urlencoded'
         }
     };
     return function (action, data) {
         var queryStr = "action=" + action;
         if (data) {
             queryStr += "&data=" + JSON.stringify(data);
         }
         request.data = queryStr;
         return $http(request);
     };
 }

2 个答案:

答案 0 :(得分:0)

应该是

func containsPoint(_ p: CGPoint) -> Bool

var image = $('<img />'); image.src = 'http://placehold.it/350x150'; image.appendTo( $('#test') ); 是变量的名称,而不是echo json_encode($postdata);

答案 1 :(得分:0)

终于找到了解决方案。 我不得不添加encodeURIComponent,因为&amp; NBSP;通过tinymce WYSIWYG添加 - 在查询字符串中将导致问题,如果没有转义。

function ($http, globals) {
            var request = {
                method: 'POST',
                url: globals.ajax_url,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            };
            return function (action, data) {
                var queryStr = "action=" + action;
                if (data) {
                    queryStr += "&data=" + encodeURIComponent(JSON.stringify(data));
                }
                request.data = queryStr;
                return $http(request);
            };