jpg jpg - 3 months ago 10
JSON Question

PHP Array to JS dont work

I'm pretty lost, all I want to do is spend a array to my javascript code. I've seen a lot of examples, but none ends me working. I need some help. I'm pretty stuck.

functions.js



function cargaTemperatura(idsem){
$.ajax({
type: "POST",
url: "load_temp.php",
data: { idsem : idsem },
dataType: "json",
success: function(data){
alert(JSON.parse(data));
},error: function(data){
alert('error');
}
});
};





load_temp.php



<?php
include_once 'Includes/db.php';
$select = "SELECT * FROM CalTmp WHERE idsem='".$_POST['id_sem']."'";
$result = mysqli_query($link, $select) or die($select);
$array = mysqli_fetch_all($result,MYSQLI_ASSOC);
debug_to_console($result->num_rows);
debug_to_console($_POST['id_sem']);
echo json_encode($array);




Answer

My suggestion:
- Step 1: In load_temp.php. Hardcode idsem value. Check on browser. Make sure there has a json string
- Step 2: Add this line

header("Content-type: application/json");

after

<?php