0

I have the following AJAX function in jQuery:

$.post('../../afisare.php', {"fac_name" : facility_name }, function(response) {
            alert(response);
});

$.ajax({
    url: "../../afisare.php",
    dataType: "JSON",
    success: function(json){
        console.log(json);
    }
});

And the following encoding in a php file:

$facility_name = $_POST['fac_name'];

$facility_data = getFacilities($facility_name);
$facility_data2 = json_encode($facility_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
print($facility_data2);

The array in the php file ($facility_data2) prints exactly as it should.

My question is: how do I create a javascript array in the succes function that contains the exact elements as the JSON?

Edit: added the $.post function also; The alert prints the JSON as it should, but the console.log(json) prints an empty array. To clarify: the JSON is generated based on what the getFacilities function returns.

Later edit: for anyone who has the same issue as I did (best advice: read the documentation thoroughly), here is the function that works:

$.post('../../afisare.php', {"fac_name" : facility_name }, function(json) {
    data = JSON.parse(json);
    console.log(data[0].facility_name);
});

The problem with my previous attempt was that I wasn't using the callback function in the $.post function as I should have (e.g. this function is directly linked to the variables that I am posting to the php file, thus being unable to be read from outside that function).

3
  • 3
    print out facility data Commented Apr 11, 2016 at 23:46
  • The funtion parameter json will contain whatever you echo out from your PHP script. I am pretty sure you dont want the JSON_PRETTY_PRINT and fairly sure you dont need the JSON_UNESCAPED_SLASHES parameters in that json_encode() function either. Because you have a dataType: "JSON" the json parameter will be converted automatically to a javascript data type matching that sent from the PHP i.e an object or an array Commented Apr 11, 2016 at 23:51
  • I used the JSON_UNESCAPED_SLASHES because the array contains some links that get messed up with backslashes. Edited the main post for clarificaitons. Commented Apr 12, 2016 at 0:06

2 Answers 2

1
var result = [];

for(var i in json)
    result.push([i, json [i]]);
Sign up to request clarification or add additional context in comments.

1 Comment

result still returns an empty array.
0
$.post('../../afisare.php', {"fac_name" : facility_name }, function(json) {
    data = JSON.parse(json);
    console.log(data[0].facility_name);
});

The problem with my previous attempt was that I wasn't using the callback function in the $.post function as I should have (e.g. this function is directly linked to the variables that I am posting to the php file, thus being unable to be read from outside that function).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.