-1

Hello i am a new coder and having first experience with JSON. I am getting values from database in php file and want to send them in javascript file using JSON object. But the problem i am facing is that i am not getting into the $.getJSON function. Here is json code in javascript file:

$(document).ready(function(){    
    $.getJSON("php/personal_profile/get_DoctorInfo.php", function (data) {    
        alert("Hello"); //NOT showing any output on the webpage.    
        if (data.login_status) 
            {    
                $('.doctor_profile').show();
                $('#name').value(data.Name);
                $('#speciality').value(data.speciality);
                $('#p_checked').value(data.p_checked);
                $('#p_pending').value(data.p_pending);  
            }
        else 
            {
                alert("You are not Logged In!");
            }    
     });    
});

In PHP file, I am just getting values from the database and trying to send them in javascript file using json_encode() function.

echo json_encode(array('login_status' => true , 'Name' => $name , 'speciality' => $speciality , 'department' => $department , 'p_checked' => $patients_checked , 'p_pending' => $patients_pending));

Please help me .. M stuck here.. If i use $.get("path",function(object){alert("hello")}); it works but when i use $.get("path",function(object){alert("hello")}, "json"); then it doesn't work.

Aatif Akhter
  • 2,126
  • 1
  • 25
  • 46

2 Answers2

1

Call must be failing to fetch data. jQuery getJSON API does take only success callback.

You can try having a fail callback to find out, if the call is throwing an exception.

jQuery getJSON is a promise and you should be able to see if the fail callback is triggered.

getJSON from jQuery documentation

$.getJSON("example.json", function() {
    console.log("success");
  })
  .done(function() {
    console.log("second success");
  })
  .fail(function(e) {
    console.log("error");
  })
  .always(function() {
    console.log("complete");
  });

jQuery.getJSON()

Sreekanth
  • 3,110
  • 10
  • 22
  • you can check the value of the argument e. that should give you more context on why it is failing. console.log(e); You can also check the request being failed on network tab of dev tools. – Sreekanth Oct 13 '16 at 06:39
  • Object {readyState: 4, responseText: "
    Parse error: syntax error, unexpect…e\get_DoctorInfo.php on line 27
    ↵", status: 200, statusText: "OK"} But i don't understand what its trying to say.
    – Hussam Cheema Oct 13 '16 at 06:46
  • It's most likely because of the response not being a json. You can see the response just by opening the URL in a new browser tab. – Sreekanth Oct 13 '16 at 06:48
  • One more thing, $.getJSON("path", function(data)){ } – Hussam Cheema Oct 13 '16 at 07:14
  • One more thing, $.getJSON("path", function(data)){ alert(data.a) } here data does not behave as an object. I am passing multiple outputs in php as u can see above. – Hussam Cheema Oct 13 '16 at 07:15
1

In jQuery getJSON ajax function is for cross origin ajax call, so natively the getJSON send you an additional get variable, I guess it's "_" in your case, because you haven't specify any callback, so at server side your response it should be look like

$_RESULT['the query string variable which you can see on console'].'('.json_encode(your array goes here).')';