1

I am trying to parse json data from online database with a php file link and i dont know how to do it. Generally i know that parsing json data is something like this.

<script>
       var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';

    var json = JSON.parse(data);

    alert(json["name"]); //mkyong
    alert(json.name); //mkyong

    alert(json.address.streetAddress); //88 8nd Street
    alert(json["address"].city); //New York

    alert(json.phoneNumber[0].number); //111 111-1111
    alert(json.phoneNumber[1].type); //fax

    alert(json.phoneNumber.number); //undefined
</script>   

But what if i create a json file with php and it has the link

www.example.com/parse.php

and i want that link to connect it with another html file in another server like

www.example2.com/parsehere.html

which has the above code without putting the json data inline, but to load it from the link of php?? Is that possible?

All im trying to do is to make inside this script to load data from a url and NOT from raw json data like the above script.

<script>
         //here something to load data from a link and parse it below.

        var json = JSON.parse(data);

        alert(json["name"]); //mkyong
        alert(json.name); //mkyong

        alert(json.address.streetAddress); //88 8nd Street
        alert(json["address"].city); //New York

        alert(json.phoneNumber[0].number); //111 111-1111
        alert(json.phoneNumber[1].type); //fax

        alert(json.phoneNumber.number); //undefined
    </script>   
9
  • 1
    you want to get json data from another server file...am i right Commented Nov 26, 2015 at 9:01
  • 1
    Instead of hard coding the json file you want get the file by referring some link, say www.example.com/parse.php .Am i right? Commented Nov 26, 2015 at 9:05
  • 1
    @ParthChavda yes and yes ! Commented Nov 26, 2015 at 9:08
  • 1
    Hey Kwnstantine, do you happen to have a more vivid example of what you are trying to do? A photo maybe? --Nikos Commented Nov 26, 2015 at 9:09
  • 1
    @Sidius Hello Niko. i will update my question with one example. Although all i am trying to do is to load json data from a link instead of putting data in the script itself Commented Nov 26, 2015 at 9:12

1 Answer 1

1

If I am reading your question right you want a php-script to generate json? Below is a part of a php-script I made that returns json.This is save as a file.php and called through the web.

$return_arr = array();

while($row = mysql_fetch_assoc($dbresult)) {
    if($row["firstletter"] != '')
    {   

        $row_array['id'] = $row['id'];
        $row_array['firstletter'] = $row['firstletter'];
        $row_array['languageid'] = $row['languageid'];
        $row_array['word'] = $row['word'];
        $row_array['description'] = $row['description'];
        $row_array['imageList'] = $row['imageList'];
         array_push($return_arr,$row_array);

    }
}

header("Access-Control-Allow-Origin: *");
header("Content-type: application/json");
$json = json_encode($return_arr);
echo $json;

To fetch your php-json use this in your code:

var HttpClient = function() {
    this.get = function(aUrl, aCallback) {
        var anHttpRequest = new XMLHttpRequest();
        anHttpRequest.onreadystatechange = function() { 
            if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
                aCallback(anHttpRequest.responseText);
        }

        anHttpRequest.open( "GET", aUrl, true );            
        anHttpRequest.send( null );
    }
}

Then to make the actual call use this:

var myClient = new HttpClient();
myClient.get('http://yourphp.php', function(response) {
    // Do what you want with the response
});
Sign up to request clarification or add additional context in comments.

2 Comments

This is my php file that i make the json file. I have already done this. All i want to do now is to parse these json data witha URL in the script of ANOTHER html file.
Also required to put error_reporting(0); in JSON output in php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.