0

I wanted to get the json code on this page .

This is the the JSON code:

[{"idStore":"13","0":"13","Name":"1414141414144","1":"1414141414144","Phone":"123456","2":"123456","Email":"tretretr","3":"tretretr","Description":"aaaaaaaaaaaa","4":"aaaaaaaaaaaa","Ville":"zaeazezae","5":"zaeazezae","Address":"aaaaaaaaaaaaaaaaaaaaaa","6":"aaaaaaaaaaaaaaaaaaaaaa","MsPoint":"0","7":"0","idGenre":"14","8":"14","Image":"--","9":"--","Country":"zaeaze","10":"zaeaze","idUser":"21","11":"21","Lat":"0","12":"0","Lng":"0","13":"0"}]

I have no knowledge of ajax so i tried to parse the content of the output of the php file into result.json.

$fp = fopen('result.json', 'w');
fwrite($fp, json_encode($stores));
fclose($fp);

I used this ajax code to read my result.json file since the script is not on the same page with the JSON output.

This is my www folder architecture:

www -> js -> app.js
    -> inedx.php
    -> Store.php
    -> result.josn

I am using app.js to load the data from the JSON file (created by the Store.php) and modify it. Then I use it in index.php.

var da = $.ajax({
    url: "../result.json",
    success: function (data) {
        var obj = JSON.parse(data);
    }
});

alert(da);

And the output was:

[Object Object]

Did i miss something?

Božo Stojković
  • 2,893
  • 1
  • 27
  • 52
  • 2
    I think you need to do `alert(obj);` in success function. As obj has the data. – Ajay Narain Mathur Jul 19 '16 at 22:14
  • also alerting an object is always going to give you `[Object Object]` due to how [Object.prototype.toString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString) works. You should log it to the console with [console.log()](https://developer.mozilla.org/en-US/docs/Web/API/Console/log) – Patrick Evans Jul 19 '16 at 22:19
  • i didn't understand you. – Abderrahman Msd Jul 19 '16 at 22:22

1 Answers1

1

In your code variable da is not the returned result but the ajax function itself.

The value of the JSON is stored in obj variable. but however its also an object and you can not just use alert for it. but however you can alert its properties like below:

var da = $.ajax({
    url: "../result.json",
    success: function (data) {
        var obj = JSON.parse(data);
         alert(obj.idStore);  //would show the idStore of the first element in Array
    }
});

If You want the alert out of the ajax function, you can do:

var obj;

    $.ajax({
        url: "../result.json",
        success: function (data) {
            obj = JSON.parse(data);
            runIt();
        }
    });

function runIt(){
 alert(obj.idStore);  
}

The other way to do it (Not Recommanded):

var obj, done = false;

    $.ajax({
        url: "../result.json",
        success: function (data) {
            obj = JSON.parse(data);
            done = true;
        }
    });

 var int = setInterval(function(){
if(done){ alert(obj.idStore); clearInterval(int);}
}, 100);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171