I have php multidimensional array which is generated dynamically
Array
(
[0] => Array
(
[cid] => 73
[type] => 2
[qrystr] => Qtest1
[trck_no] => (570) 244-3738
[trgt_no] => 1919674683063
[webpage] => Array
(
[0] => Array
(
[wid] => 40
[page_url] => www.ctest2.com
)
[1] => Array
(
[wid] => 41
[page_url] => www.ctest3.com
)
)
)
[1] => Array
(
[cid] => 75
[type] => 3
[qrystr] => Qtest6
[trck_no] =>
[trgt_no] =>
[webpage] => Array
(
[0] => Array
(
[wid] => 42
[page_url] => www.test1.com
)
[1] => Array
(
[wid] => 43
[page_url] => www.test1.com
)
)
)
)
and I want to convert it in Javascript. For that I have already encoded it using json_encode()
in php and here is the output for the same :
[{"cid":"73","type":"2","qrystr":"Qtest1","trck_no":"(570) 244-3738","trgt_no":"1919674683063","webpage":[{"wid":"40","page_url":"www.ctest2.com"},{"wid":"41","page_url":"www.ctest3.com"}]},{"cid":"75","type":"3","qrystr":"Qtest6","trck_no":"","trgt_no":"","webpage":[{"wid":"42","page_url":"www.test1.com"},{"wid":"43","page_url":"www.test1.com"}]}]
In javascript whenever I parse this using JSON.parse()
I get an error message (i.e. SyntaxError: JSON.parse: unexpected character).
Kindly suggest.
EDITED:
My php file is in separate server and where pogramatically I gather information from database based on "id" and store it in an array.
Here is my php code:
foreach($campaigns as $campaign){
if(($campaign->type == 2) || ($campaign->type == 3)){
foreach($campaign->webpage as $webpage){
$webpages[] = array(
'wid'=>trim($webpage->id),
'page_url'=>trim($webpage->page_url)
);
}
}else{
$webpages = "";
}
$campaign_details[] = array(
'cid'=>trim($campaign->id),
'type'=>trim($campaign->type),
'qrystr'=>trim($campaign->qrystr),
'trck_no'=>trim($campaign->trcknmb->frnd_name),
'trgt_no'=>trim($campaign->trcknmb->trgtnmb->phone_no),
'webpage'=>$webpages
);
unset($webpages);
}
$cdetails = json_encode($campaign_details);
echo $cdetails;
I make a cross domain request to get the array valu.
Here is my Javascript code to parse the json string:
function handler(evtXHR)
{
if (invocation.readyState == 4)
{
if (invocation.status == 200)
{
response = invocation.responseText;
var content = JSON.parse(response);
alert(content);
}
else
{
alert("Invocation Errors Occured");
}
}
}
I put an alert function here to check the output result.