AnsweredAssumed Answered

AJAX Returns Incorrect data

Question asked by Haroon Khaleeq on Oct 24, 2014
Latest reply on Oct 27, 2014 by Haroon Khaleeq
MERGED
This conversation has been merged. Please reference the main conversation:
Hi,

I made an ajax call to retrieve some data. The response is JSON encoded data but it doesn't return JSON response instead it gives me this: Sometimes it works fine some times it does not.....

===================================================
"<script>            if (parent.location == window.location) {
                window.location = "http:\/\/localhost\/imerge\/#Leads";
            } else {
                window.top.SUGAR.App.bwc.login("index.php?module=Leads&action=getPhonePT&sugar_body_only=true&id=501d59fd-caaa-e57c-3270-5447a7b25788");
            }
</script>"
====================================================

Well, sometimes it works when i reload the page or when i repair and rebuild the same code.
The ajax call and php code is given below for your better understanding:

jQuery.ajax({
        url: "index.php?module=Leads&action=getPhonePT&sugar_body_only=true",
        type: "GET",
  dataType: "JSON",   
        data: "id="+this.model.id,
   success: function(data)
   {     
    //success   
   },
   error: function(msg)
   {
    alert("Problem in Data Accessing. Try Reloading the Page.");
    //it comes here....!!!
   }
  });



======================================================


 header('Content-Type: application/json');  //print_r($_REQUEST);
  $GLOBALS['log']->fatal(print_r($_REQUEST,true));
  
  $lead_id = $_REQUEST['id'];
  //echo $lead_id;

  $tableName = "phone_pt";
  $relatedPhone = array();
  
  if(!empty($lead_id)){
$result = $GLOBALS['db']->query("SELECT * FROM $tableName where parent_id='$lead_id'");
//print_r($result);
if($result->num_rows > 0){
  while($row = $GLOBALS['db']->fetchByAssoc($result)){
   //print_r($row);
   $relatedPhone[] = $row;
  } 
}  
  }
  //echo "Hello123";
  // $GLOBALS['log']->fatal(print_r($relatedPhone,true));
  echo json_encode($relatedPhone);  


Thanks

Outcomes