doukuilian8365 2014-04-06 04:57
浏览 246
已采纳

提取json.stringify(数据)

I'm building an API, and I need a little help, to understand how can I parse data from JSON request, here is my code:

<textarea style="width: 100%; height: 300px;" id="request_json">
{
  "requestType":"TourListRequest",
  "data":{
  "ApiKey":"12345",
  "ResellerId":"999",
  "SupplierId":"999",
  "ExternalReference":"12345",
  "Timestamp":"2013-12-10T13:30:54.616+10:00",
  "Extension":{
  "any":{
      }
   },
  "Parameter":{
  "Name":{
    "0":" "
   },
  "Value":{
    }
   }
 }
}
 </textarea>

<script>
 function SubmitAPI() {
    var sendInfo = { 
      JSON    : $('#request_json').val(),
      URL     : $('#supplier_api_endpoint_JSON_Tour_List').val(),
      Type    : 'JSON Tour List'
    };

$('#response_json').html("Calling API...");
$.ajax({
       url: 'post_JSON.php', 
       type: "POST",
       data: JSON.stringify(sendInfo), // send the string directly
       success: function(response){
      var obj = jQuery.parseJSON( response );
    $('#response_json').html(response);
    $('#response_validation').html( obj.json_valid );
          },
          error: function(response) {
          $('#response_json').html(response);
           }
        }); 
   }
  </script>

So I need to know how to receive "JSON.stringify(sendInfo)" in my php script post_JSON.php

Any idea ?

Thank you in advance,

  • 写回答

2条回答 默认 最新

  • dou1908 2014-04-06 05:26
    关注

    I think you need to name you data string, something like...

    data: {info: JSON.stringify(sendInfo)},
    

    and in your php:

    $json_data = $_POST['info'];
    var_dump(json_decode($json_data, true));
    

    to get at that data using php, do something like:

    $postedData = json_decode($json_data, true); // turns json string into an object
    $requestType = $postedData['requestType']; 
    

    if you need to parse a returned json string with jquery you do something like this:

    var jsonStuff = jQuery.parseJSON( data );
    alert( jsonStuff.requestType);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)