NiTHiN RaJ
NiTHiN RaJ

Reputation: 38

How to read an array which is received as Ajax response text in JavaScript

The response text I am getting after ajax call.

$ Array(
    [0] => Array
        (
            [trade_type] => Carpentry
            [active_status] => 0
            [work_order_received] => 8912629
            [fault_type] => 
        )

    [1] => Array
        (
            [trade_type] => Carpentry
            [active_status] => 1
            [work_order_received] => 8912629
            [fault_type] => 
        )

)

As this has multiple entries how to loop through this and get data. for example : var trade_type = carpentry;

I tried using JSON.pase. but this started to return single character at a time

My Php code which is returning Ajax data.

public function get_job_details_by_workorder(){

$workorder = $this->input->post('work_order');
$query=$this->db->query("select trade_type,active_status,work_order_received,fault_type from nrm_doc_header where work_order_received='$workorder'")->result_array();

print_r($query);
}

Upvotes: 2

Views: 80

Answers (2)

NiTHiN RaJ
NiTHiN RaJ

Reputation: 38

I was missing dataType:Json in my ajax call. so i was getting return in text format.so was not able to parse it.

Upvotes: 0

charlietfl
charlietfl

Reputation: 171679

You need to json_encode() it in php first so it gets sent as as [{"trade_type":"Carpentry"...}]

echo json_encode($myArray);

Upvotes: 1

Related Questions