首页 > 解决方案 > 如何在ajax中将字符串保持为原始格式?

问题描述

我通过 ajax 请求发送一些值,如下所示:

    $('#compileBtn').on('click', function(evnt){
            evnt.preventDefault();
            var souceCode = cEditor.getValue();
            console.log(souceCode);
      var button = $(this),
          ajax_url = '<?php echo admin_url("admin-ajax.php"); ?>',
          data = {
              action: 'get_output_by_ajax',
              sourceCode : souceCode,
              lang: 2,
              testcases: ["1"],
              security: '<?php echo wp_create_nonce("get_output"); ?>'
          };

      $.post(ajax_url, data, function (response) {
                // obj = JSON.parse(response);
                 console.log(response);
      }).always(function () {
          $('.result-processing').addClass('hide');
      });

    });

我只是将收到的值返回如下: function get_states_by_ajax_callback(){

  check_ajax_referer('get_output','security');
  $sourceCode = $_POST['sourceCode'];

   echo $sourceCode; wp_die();
}

看看,我使用了两个console.log()分别在ajax请求之前和通过console.log(souceCode);和 获取ajax请求之后打印值console.log(response);。但souceCode已更改如下: 发送ajax请求之前:

 #include<iostream>
     using namespace std;
     int main()
     {
     cout<<"Hello Wold!"<<endl<<"How are you?";
      return 0;
      }

收到ajax请求后

#include<iostream>
 using namespace std;
 int main()
 {
 cout<<\"Hello Wold!\"<<endl<<\"How are you?\";
  return 0;
  }

这意味着在“之前添加反斜杠。我怎样才能保持不变或将其转换为原始形式?

标签: jsonajax

解决方案


推荐阅读