必承其重 | 欲带皇冠 2017-06-20 15:08 采纳率: 0%
浏览 32

使用Ajax获取JSon数据

can you guys help me, I am not really a JS dev so i am polling my hair out with this one. i'm working with Ajax to display data from a json file...but it doesn't work, i put console.log almost everywhere to see result but it won't work, here's my code:

encode data:

// Inclusion du ficher de fonction permettant de démarrage du contexte de l'applicaiton
Include $_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR."Includes".DIRECTORY_SEPARATOR."Functions".DIRECTORY_SEPARATOR."functions.php";

// Initialisation des variables
$action     = '';
$pValue     = '';
$pType      = '';
$pRegion    = '';
$currenthyperviseur = new hyperviseur();
$currentregion      = new region();

// Récupération de l'action passée au webservice
if(isset($_REQUEST['action']) && $_REQUEST['action']!= "")      {$action = $_REQUEST['action']; }
if(isset($_REQUEST['pValue']) && $_REQUEST['pValue']!= "")      {$pValue = base64_decode($_REQUEST['pValue']); }
if(isset($_REQUEST['pType']) && $_REQUEST['pType']!= "")        {$pType = $_REQUEST['pType']; }
if(isset($_REQUEST['pRegion']) && $_REQUEST['pRegion']!= "")    {$pRegion = $_REQUEST['pRegion']; }

// Gestion de l'ensemble des cases du web service
switch($action)
{
    //
    // Permet la récupération de données dans l'Hyperviseur (GET)
    //
    case 'H_getAlmBacPFull'     : echo json_encode($currenthyperviseur->getListAlarmeBac(1));           break;
    case 'H_getAlmBacTFull'     : echo json_encode($currenthyperviseur->getListAlarmeBac(2));           break;
    case 'H_getGrpBacTFull'     : echo json_encode($currenthyperviseur->getListAlarmeQualifBac());      break;

    case 'H_getAlmDetails'      : echo json_encode($currenthyperviseur->getAlarmeDetails($pValue));     break;
    case 'H_getGrpDetails'      : echo json_encode($currenthyperviseur->getGroupeDetails($pValue));     break;

    //
    // Permet la MAJ de données dans l'Hyperviseur (SET)
    //
    case 'H_setCheckAlm'        : echo $currenthyperviseur->setAlarmeCheckAction($pValue);              break;
    case 'H_setGroupAlm'        : echo $currenthyperviseur->setGroupAlarme($pValue);                    break;
    case 'H_setAlmIdGrp'        : echo $currenthyperviseur->setAlarmeIdGrp($pValue);                    break;


    //
    // Permet la récupération de données pour le module 'HyperViseur MoreDetails' (GET)
    //
    case 'HVMD_getContentBloc'  : echo json_encode($currenthyperviseur->getContentBloc($pType, $pValue));   break;


    //
    // Permet la récupération de données pour le module 'Region' (GET)
    //
    case 'R_getListContentBloc' : echo json_encode($currentregion->getListContentBloc($pType));         break;
    case 'R_getContentBloc'     : echo json_encode($currentregion->getContentBloc($pType, $pRegion));   break;
        //
    // Permet la récupération de données pour le module la map des régions
    //
    case 'dashMap_getSite'  : echo json_encode($dashMap_getSite($pRegion);          break;


}

function dashMap_getSite($pRegion) to get data:

    $dataArray = array();
    if($pRegion == 'all')
    {
        $dataLine = array();
        $dataLine['codeSite']   = "01014";
        $dataLine['latitude']   = '46.2625';
        $dataLine['longitude']  = '5.6380';
        $dataLine['nom']        = 'Oyonnax 2';
        $dataLine['nbAlm']      = 10;
        $dataLine['critilevel'] = 2;
        array_push($dataArray, $dataLine);

        $dataLine = array();
        $dataLine['codeSite']   = "04025";
        $dataLine['latitude']   = '44.4242';
        $dataLine['longitude']  = '6.7533';
        $dataLine['nom']        = 'Barcelonette 3 Est TDF';
        $dataLine['nbAlm']      = 0;
        $dataLine['critilevel'] = 1;
        array_push($dataArray, $dataLine);
    }
    return $dataLine;

And this second function getArrayOfSiteAjax() to display it with ajax

        console.log(gWsProviderUrl);
        $.ajax({
            url: gWsProviderUrl + '?action=dashMap_getSite&pRegion=all',
            type:  'POST',
            dataType: 'json',
            success:function(data){
        console.log('arrayOfSite');
                arrayOfSite = data;

                $.each(arrayOfSite, function(currentIdx, currentValue){

                    console.log('codeSite   => ' + currentValue.codeSite );
                    console.log('latitude   => ' + currentValue.latitude );
                    console.log('longitude  => ' + currentValue.longitude );
                    console.log('nom        => ' + currentValue.nom );
                    console.log('nbAlm      => ' + currentValue.nbAlm );
                    console.log('critilevel => ' + currentValue.critilevel);
                });
                console.log('heyy');
            }
        });

Thank you !

  • 写回答

1条回答 默认 最新

  • csdn产品小助手 2017-06-20 16:22
    关注
    case 'dashMap_getSite'  : echo json_encode($dashMap_getSite($pRegion);          break;
    

    That's a syntax error and should probably be

    case 'dashMap_getSite'  : echo json_encode(dashMap_getSite($pRegion));          break;
    

    Because there was an error, the success callback didn't get called, an you don't get a message.

    To display the data, you could do something like this:

    // ...
    success: function (data) {
      var container = $('#container'); // the element in which you want to display the data
      // loop over each item in the array
      $.each(data, function (index, element) {
        var outer = $('<div />');
        // element is an object, loop over its properties
        $.each(element, function (key, value) {
          var inner = $('<div />');
          inner.text(key + ': ' + value);
          outer.append(inner);
        })
        container.append(outer);
      });
    }
    // ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'