-3

i have created a dynamic array and then, i convert it to json object. The code is the follows:

$array_D1[]="";
$array_D2[]="";
$array_to_send[]="";

$array_D1[$CapaEnviar][] = $fila['test_name'];
$array_D2[]=$seleccio_content;
$array_D2[]=$id_capa;
$array_D2[]=$pagina;
$array_D2[]=$idtest;
$array_D2[]=$valor_org;
$array_D2[]=$valor_eval;
$array_D2[]=$valor_test;

$array_to_send=array('data1' => $array_D1, 'data2' => $array_D2);

echo json_encode($array_to_send,true);

This code is a AJAX response for an AJAX request. I don't know how i can access JSON data from jquery.

i need to acces the values from "data1" and "data2"

riztak
  • 55
  • 2
  • 8
  • You forgot to include the client side code. We don't know how you are fetching the data. – Quentin May 10 '16 at 14:07
  • PHP defaults to sending a `Content-Type: text/html` response header. You need to override that with the `header()` function if you aren't outputting HTML. – Quentin May 10 '16 at 14:07
  • `$(document).on('change', '#seleccio_test,#seleccio_test2', function(){ var capaTEST; var pagina="show_test_prova.php"; $.ajax({ async:false, cache:false, dataType:"json", type: "POST", url: pagina, data: "dades_test="+obtenirTest(), success: function(obj){ var con= $("#KTK"); con.html(obj.data2); }, beforeSend:function(){}, error:function(jqXHR, textStatus, errorThrown ){alert("Error: "+textStatus);} }) })` – riztak May 10 '16 at 14:09
  • That looks like it should work. What's the problem? – Quentin May 10 '16 at 14:12
  • Try parse the json JSON.parse(obj); – Johnny John May 10 '16 at 14:13
  • 1
    @JohnnyJohn — No. It says `dataType:"json"`. jQuery will parse it before populating `obj`. – Quentin May 10 '16 at 14:14
  • the client side code is a simple AJAX request , i want to access each one of data that are there both data1 as data2, it's clear? – riztak May 10 '16 at 14:14
  • @riztak — No, it isn't clear. You're just repeating your goal without explaining what the problem is. The code appears to already successfully access data2. It should be obvious how to access data1. What specific problem are you having? – Quentin May 10 '16 at 14:16
  • ok, wait a moment i will do some test . – riztak May 10 '16 at 14:22
  • This works good: alert(JSON.stringify(obj.data1));, both data1 and data2 gives an array , what i want is to loop in order to obteain the datas from eah one . – riztak May 10 '16 at 14:31
  • i need to acces each one of value inside data1 and data2 – riztak May 10 '16 at 14:38

1 Answers1

-1

I'm not sure what's hard about this. The others are right that you probably should add:

header('Content-type: application/json');

to your php file before your echo statement.

Assuming that you can access the php with a url, say, http://blah.com/test.php, you can...

$.ajax({
        method: 'post', //or 'get'
        url: 'http://blah.com/test.php'
})
.done(function(data) {
    console.dir(data.data1);
    console.dir(data.data2);
})
.fail(function(jqXHR, textStatus) {
    console.dir(jqXHR);
    console.dir(textStatus);
});

Obviously, you can replace the body of the callback with whatever processing you want.

tqwhite
  • 658
  • 7
  • 16