JSON

Username (e.g. epiz_XXX) or Website URL

http://binita.infinityfreeapp.com/

Error Message

(please share the FULL error message you see)

Other Information

Hi,
I am trying to get the data from the geojson file using php file_get_content but for some reason it doesn’t show up on my website. When i try to load just the geojson file it works but it doesn’t work on the website itself.
So on my console.log it shows:
{“status”:{“code”:“200”,“name”:“ok”,“description”:“success”,“returnedIn”:“15 ms”},“data”:}
I should be getting the geojson data in “data” but its empty.

Can you please give a lot more info, as well as the code? We really can’t help you if we only get a “doesn’t work” and the console output.

So I am trying to get the geojson data in javascript using ajax through php. This is my php file:

<?php  

ini_set('display_erros', 'On');

error_reporting(E_ALL);

$executionStartTime= microtime(true);

 
//$url= file_get_contents("https://binita.infinityfreeapp.com/countryBorders.geo.json");

$url=file_get_contents("countryBorders.geo.json");
    

                $decode = json_decode($url,true);

    $newArr = [];

   

    for($i=0; $i<count($decode['features']); $i++) {

    array_push($newArr, $decode['features'][$i]['properties']);



}

//print_r($decode);

                $output['status']['code'] = "200";

                $output['status']['name'] = "ok";

                $output['status']['description'] = "success";

                $output['status']['returnedIn'] = intval((microtime(true) - $executionStartTime) * 1000) . " ms";

    $output['data'] = $newArr;

    //$output['data'] = $decode['features'];

   

    header('Content-Type: application/json; charset=UTF-8');

 

echo json_encode($output);

 

?>

Javascript:

$(document).ready(function(){
    $.ajax({
			url: "libs/php/task.php",
			type: 'GET',
			dataType: 'json',
            			data: {
				name: $('#countries').val(),
				iso_a2: $('#country').val()
                
			},
			success: function(result) {
				console.log(JSON.stringify(result));
				if (result.status.name == "ok") {
                var data=result.data;
                console.log(data);
                      var formoption = "";

                $.each(data, function(v) {
    var val = data[v]
     optionText = 'Choose Country';
            optionValue = 'Choose Country';
       //formoption += "<option value='" + optionValue + "'>" + "Ch" + "</option>"; 
    formoption += "<option value='" + val.iso_a2 + "'>" + val.name + "</option>";
  });
  $('#countries').html(formoption);    
              
			}
			
			},
			error: function(jqXHR, textStatus, errorThrown,err){
				// your error code
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
console.log(err);

			}
		}); 
	
	});

Can you please format your code?

Please highlight the code (Not your explanatory text) and click the </> icon on the top of the editor.
Thanks

I updated the formatting on your post a bit to make the code easier to read.

Checking your website, is this supposed to be the URL to your PHP script? https://binita.infinityfreeapp.com/libs/php/task.php?name=&iso_a2=

Because it returns the data field with all the content I would expect.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.