Member Avatar for michael.dewitt.716

Hi everyone,
i have a page that uses jquery ajax to get json from another page. However, instead of the ajax using sucess, it goes directly to error. I have a debug on the error to see what is the issue and i get "Parsing JSON Request failed."

page 1.php

$("#validate").click(function() { var tags = $("#coc_tag").val(); var type = $("#type").val(); var cleantag = tags.replace(/#/, "%23"); switch(type) { case 'clan': $.ajax({ url: "/coc/mirror.php?tag="+cleantag+"&type="+type, method: "GET", //dataType: "json", cache: false, success: function(data, textStatus){ //for testing. alert("good"); $.each(data,function( key, value ) { alert( key + ": " + value ); }) }, error:function(x,e) { if (x.status==0) { alert('You are offline!!\n Please Check Your Network.'); } else if(x.status==404) { alert('Requested URL not found.'); } else if(x.status==500) { alert('Internel Server Error.'); } else if(e=='parsererror') { alert('Error.\nParsing JSON Request failed.'); } else if(e=='timeout'){ alert('Request Time out.'); } else { alert('Unknow Error.\n'+x.responseText); } } }); break; } });

here is the php page the outputs the json.
mirror.php

<?php header('Content-type: application/json'); // extra curl stuff that i took out $headr = array(); $headr[] = "Accept: application/json"; $headr[] = "Authorization: Bearer ".$token; curl_setopt($ch, CURLOPT_HTTPHEADER, $headr); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch); $data = json_decode($res, true); $items = $data["items"]; class item { public $tag = ""; } $test = new item(); foreach($items as $item) { $test->tag = $item["tag"]; echo json_encode($test); } curl_close($ch); ?>

The output looks like this

{"tag":"#VGLVGVVJ"}{"tag":"#Y0G8PC2G"}{"tag":"#90UVCJUU"}{"tag":"#LG9L8GJG"}{"tag":"#J80R2VLJ"}{"tag":"#9PP298LR"}{"tag":"#Q0JYV8JU"}{"tag":"#9JY2U2YY"}{"tag":"#LPUUG2Y9"}{"tag":"#GL9929UC"}{"tag":"#PG0QQJ09"}{"tag":"#9PLR09G0"}{"tag":"#GCJPQ92Y"}{"tag":"#UC00RCLJ"}{"tag":"#2LR8UVLJ0"}{"tag":"#RVUYGCPV"}{"tag":"#2LJLQGYGG"}{"tag":"#RQ22PJYC"}{"tag":"#PP0P9VUL"}{"tag":"#8RVCV0RV"}{"tag":"#QL88L9CJ"}{"tag":"#RUCLLUV"}{"tag":"#2Q0PQCPV"}{"tag":"#RYLJJU02"}{"tag":"#GVGJLQU9"}{"tag":"#L8VLQGC9"}{"tag":"#2288P98Q8"}{"tag":"#2CG22U002"}{"tag":"#VGJQYQQ9"}{"tag":"#2ULCPY2G0"}{"tag":"#229GP9C09"}{"tag":"#82RPULG2"}{"tag":"#VU82QUCY"}{"tag":"#202JCRL9"}{"tag":"#VCR8PY2V"}{"tag":"#8YLC2LJCR"}{"tag":"#GUQJQU90"}{"tag":"#RPQ0U2Y0"}{"tag":"#2LLGC8C8V"}{"tag":"#22CPJRPPQ"}{"tag":"#UCUL0RR0"}{"tag":"#2LC82CL8"}{"tag":"#2RJQ9P28C"}{"tag":"#2QPCYY9U"}{"tag":"#P2VU8YG0"}{"tag":"#20RGCLCL"}{"tag":"#2YJG2R88C"}

Again, im getting the error trigger on the jQuery ajax.

Member Avatar for diafol

Not sure I understand. Are you getting data from a non-ajax run of the php script? If you had an ajax error, then you wouldn't get the data.

Anyway - this isn't valid json as you are returning multiple objects. If you added all the objects to an array that should work - then you can json_encode that and use the dataType: "json" in your ajax setup.

Member Avatar for diafol

There must be a simpler way of getting the data. A simple data structure to pass back may be:

["#VGLVGVVJ","#Y0G8PC2G","#90UVCJUU","#LG9L8GJG","#J80R2VLJ","#9PP298LR","#Q0JYV8JU","#9JY2U2YY","#LPUUG2Y9","#GL9929UC","#PG0QQJ09","#9PLR09G0","#GCJPQ92Y","#UC00RCLJ","#2LR8UVLJ0","#RVUYGCPV","#2LJLQGYGG","#RQ22PJYC","#PP0P9VUL","#8RVCV0RV","#QL88L9CJ","#RUCLLUV","#2Q0PQCPV","#RYLJJU02","#GVGJLQU9","#L8VLQGC9","#2288P98Q8","#2CG22U002","#VGJQYQQ9","#2ULCPY2G0","#229GP9C09","#82RPULG2","#VU82QUCY","#202JCRL9","#VCR8PY2V","#8YLC2LJCR","#GUQJQU90","#RPQ0U2Y0","#2LLGC8C8V","#22CPJRPPQ","#UCUL0RR0","#2LC82CL8","#2RJQ9P28C","#2QPCYY9U","#P2VU8YG0","#20RGCLCL","#2YJG2R88C"]

That is, a simple array of what looks like id values.

Maybe...

$data = $data = json_decode($res, true); $array = array_column($data['items'], 'tag'); echo json_encode($array);

No need for loops.

commented: "There has to be a better way!" (brought to you by Diafol)+12
Member Avatar for michael.dewitt.716

Ok sorry, it was late last night when i posted this. Here is the JSON response from clash of clans website:

<?php ["items"]=> array(45) { [0]=> array(10) { ["tag"]=> string(9) "#VGLVGVVJ" ["name"]=> string(5) "ariel" ["role"]=> string(8) "coLeader" ["expLevel"]=> int(203) ["league"]=> array(3) { ["id"]=> int(29000020) ["name"]=> string(15) "Titan League II" ["iconUrls"]=> array(3) { ["small"]=> string(94) "https://api-assets.clashofclans.com/leagues/72/llpWocHlOoFliwyaEx5Z6dmoZG4u4NmxwpF-Jg7su7Q.png" ["tiny"]=> string(94) "https://api-assets.clashofclans.com/leagues/36/llpWocHlOoFliwyaEx5Z6dmoZG4u4NmxwpF-Jg7su7Q.png" ["medium"]=> string(95) "https://api-assets.clashofclans.com/leagues/288/llpWocHlOoFliwyaEx5Z6dmoZG4u4NmxwpF-Jg7su7Q.png" } } ["trophies"]=> int(4427) ["clanRank"]=> int(1) ["previousClanRank"]=> int(1) ["donations"]=> int(1281) ["donationsReceived"]=> int(907) } [1]=> array(10) { ["tag"]=> string(9) "#90UVCJUU" ["name"]=> string(9) "Satanillo" ["role"]=> string(6) "leader" ["expLevel"]=> int(171) ["league"]=> array(3) { ["id"]=> int(29000018) ["name"]=> string(17) "Champion League I" ["iconUrls"]=> array(3) { ["small"]=> string(94) "https://api-assets.clashofclans.com/leagues/72/9v_04LHmd1LWq7IoY45dAdGhrBkrc2ZFMZVhe23PdCE.png" ["tiny"]=> string(94) "https://api-assets.clashofclans.com/leagues/36/9v_04LHmd1LWq7IoY45dAdGhrBkrc2ZFMZVhe23PdCE.png" ["medium"]=> string(95) "https://api-assets.clashofclans.com/leagues/288/9v_04LHmd1LWq7IoY45dAdGhrBkrc2ZFMZVhe23PdCE.png" } } ["trophies"]=> int(3936) ["clanRank"]=> int(2) ["previousClanRank"]=> int(3) ["donations"]=> int(247) ["donationsReceived"]=> int(318) } } ?>

Just want to get the name and tag of each member and return them with json_encode back to page1.php.
i re-added the dataType: "json", and with your answer i was able to get a success response. Thanks alot on that.
I have been looking about about trying to loop through multidimenstional associative array, but without using foreach - from my level of knowledge - i wont be able to return them all without actually having the build the structure.
crude example:
echo "{"
for($i=0;$i<51;$i++)
echo "\"tag\"=>".$array = array_column($data['items'], 'tag').",";
echo "}";

Member Avatar for michael.dewitt.716

Just want to say thanks diafol. Given the above output from above, i will work on this and post what ever i come up with.
i guess for night now, i can use each $k=>$v, but im still gonna tinker around. Thanks for the reply.

Member Avatar for diafol

Hi MIke - this simple 4-line function may be useful:

Shove in your json_decoded array (items level) and a mask array that has a list of the top level fields you need. This could be done just as easily with a individual items in a foreach loop, but intersect helps to simplify the code - may be marginally slower though.

//Simple top-level retriever function cherryPick(Array $dataArray, Array $maskArray) { $result = []; $myMask = array_flip($maskArray); foreach($dataArray as $item) $result[] = array_intersect_key($item, $myMask); return $result; } $mask = ['name', 'tag', 'trophies']; echo "<pre>"; print_r(cherryPick($data['items'], $mask));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.