最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - ajax response strips multidimensional array and unable to decode

programmeradmin2浏览0评论

I am using the wp_ajax_ and wp_ajax_nopriv_ in localize script I am passing a big multidimensional array( it's big!). It works without any JSON encode, decodes. But I want to encode it is while I make ajax call...so I used json_encode in localizing script (wp_json_encode also gives the same output).. I pass it through ajax data parameter. now when I access that as
json_decode($_POST['largejson'], true)
i get the response but not as an array but a string with lot of backslashes
\":[{\"field_type\":\"shortcode_elementor_pro\",\"_id

what could be possibly going wrong?

update i use this to debug it

if ($data_settings === null){
        switch (json_last_error()) {
          case JSON_ERROR_NONE:
              echo ' - No errors';
          break;
          case JSON_ERROR_DEPTH:
              echo ' - Maximum stack depth exceeded';
          break;
          case JSON_ERROR_STATE_MISMATCH:
              echo ' - Underflow or the modes mismatch';
          break;
          case JSON_ERROR_CTRL_CHAR:
              echo ' - Unexpected control character found';
          break;
          case JSON_ERROR_SYNTAX:
              echo ' - Syntax error, malformed JSON';
          break;
          case JSON_ERROR_UTF8:
              echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
          break;
          default:
              echo ' - Unknown error';
          break;
          }

it showed - Syntax error, malformed JSON'; so json_encode might be the problem maker

I am using the wp_ajax_ and wp_ajax_nopriv_ in localize script I am passing a big multidimensional array( it's big!). It works without any JSON encode, decodes. But I want to encode it is while I make ajax call...so I used json_encode in localizing script (wp_json_encode also gives the same output).. I pass it through ajax data parameter. now when I access that as
json_decode($_POST['largejson'], true)
i get the response but not as an array but a string with lot of backslashes
\":[{\"field_type\":\"shortcode_elementor_pro\",\"_id

what could be possibly going wrong?

update i use this to debug it

if ($data_settings === null){
        switch (json_last_error()) {
          case JSON_ERROR_NONE:
              echo ' - No errors';
          break;
          case JSON_ERROR_DEPTH:
              echo ' - Maximum stack depth exceeded';
          break;
          case JSON_ERROR_STATE_MISMATCH:
              echo ' - Underflow or the modes mismatch';
          break;
          case JSON_ERROR_CTRL_CHAR:
              echo ' - Unexpected control character found';
          break;
          case JSON_ERROR_SYNTAX:
              echo ' - Syntax error, malformed JSON';
          break;
          case JSON_ERROR_UTF8:
              echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
          break;
          default:
              echo ' - Unknown error';
          break;
          }

it showed - Syntax error, malformed JSON'; so json_encode might be the problem maker

Share Improve this question edited Jan 13, 2020 at 14:19 asked Jan 13, 2020 at 13:56 user145078user145078
Add a comment  | 

1 Answer 1

Reset to default 0

This fixed my issue $set = stripslashes($set);

发布评论

评论列表(0)

  1. 暂无评论