2014-03-31 3 views
2

MySQL 데이터베이스에서 데이터를 수집하는 데 JQuery Ajax를 사용하고 있습니다. 내가 변수에 첫 번째 배열을 할당 할 수 있습니다, 내 아약스 호출 :JQuery Ajax : 단일 요청을 사용하여 두 변수에 응답을 지정하십시오.

["Vendas","Services","Other"][{"name":"Oçamento","data":[14000,8500,0]},{"name":"Real","data":[3738,11120.99,15000]}] 

내 질문은 다음과 같이

$categorias= array_map(function($a) { return ($a["categorias"]); }, $rows); 
$real = array_map(function($a) { return floatval($a["real"]); }, $rows); 
$orcamento = array_map(function($a) { return floatval($a["orcamento"]); }, $rows); 

echo json_encode($categorias); 

echo json_encode(array(
    array(name => 'Orçamento', data => $orcamento), 
    array(name => 'Real', data => $real) 
)); 

이 두 배열을 반환 :

내 PHP 스크립트는 아래와 같다 그리고 두 번째 다른 변수? 방법?

이는 하나의 Ajax 및 서버 요청 만 사용한다는 것을 의미합니다.

현재 내가 가진 :

echo json_encode(array(
'categorias' => $categorias, 
'real' => array(name => 'Orçamento', data => $orcamento), 
'orcamento' => array(name => 'Real', data => $real) 
)); 

그리고 JS에서 :

$.ajax({ 
      dataType: 'json', 
      url: '../myData.php', 
      success: function (response) { 
       dados = response; 
       (...) 
      } 
    }); 

답변

2

당신은이 구문을 사용할 수 있습니다

$.ajax({ 
      dataType: 'json', 
      url: '../myData.php', 
      success: function (response) { 
       categorias = response.categorias; 
       real = response.real; 
       (...) 
      } 
    }); 
+0

그것은 의미가 있습니다, 여기에 문제는이 것입니다 나에게 3 개의 물건을 줘. 그러나, 나는 단지 두 가지를 neeed : 첫 번째 categoias 및 두 번째 (실제 및 orçamento) 내 질문에 설명 된. –

+1

구문을 사용하여 해결책을 찾을 수있었습니다 .-D –

+1

이것은 단지 하나의 응답으로 데이터를 조합 할 수없는 예입니다. – Barif

관련 문제