-1

Can somebody can tell me why my code will suddenly callback to error and suddenly can successful randomly?Thanks.

function get_timeframe(){

    var v_fldname = "xUPH_exclude_Timeframe";

        if ($.trim(v_fldname) != '') {  
        //alert(v_fldname);                 
        $.ajax({
        url:"../ajax/get_timeframe.php",
        dataType: "json",
        data:{v_fldname: v_fldname},
        success: function(data) {

              if ( data.result != null ) {
                $.each(data.result, function(){ 

                    var code_value = this['code_value'];                    
                    document.getElementById('v_xUPH_exclude_Timeframe').value = code_value;
                    //alert(" get v_xUPH_Scan_Count");  
                });
            }           
        },
        error: function(data) {
        alert("get_timeframe error");
        }
        });                 
    }   
}

The following php code.

if (isset($_REQUEST['v_fldname']) === true) { require '../Connections/con_meditop.php';

$query = mysql_query("
    SELECT  code_mstr.code_value
    FROM    code_mstr
    WHERE   code_mstr.code_fldname = '" . mysql_real_escape_string(trim($_REQUEST['v_fldname'])) . "'
"); 


$result = array();          
if(mysql_num_rows($query) == 0)
{
        $result = null;
}else{


while ( $row = mysql_fetch_array($query) )
    array_push($result, array('code_value' => $row[0]));

echo json_encode(array("result" => $result));   

}

}

  • 2
    I don't know. Maybe looking at the actual error message tells you why. – Felix Kling Oct 01 '14 at 01:54
  • Open the browser's Javascript console, go to the Network tab, load the page, then run the action that triggers the AJAX request. Do this a few times maybe until you see why you're getting an error. It should be in the response code of the response header. – Jared Farrish Oct 01 '14 at 02:00
  • sometime can retrive the record but sometime will callback to error and display the error message that i set it before. – Szemin Goh Oct 01 '14 at 02:10

1 Answers1

0

The issue is on the receiving end of your Ajax call--get_timeframe.php. Press F12 and click the network part of the console, then send the Ajax call. You'll see get_timeframe.php show up in the network console and then turn red. Click it and look at the response body.

Or instead of alert("get_timeframe error"); Do console.log(data); to see a run-down of the error in the console (press F12).

Chad Hedgcock
  • 11,125
  • 3
  • 36
  • 44
  • it show the argument is null when callback to error. "
    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/Meditop/ajax/load_part_uph.php on line 18
    {"result":null}"
    – Szemin Goh Oct 01 '14 at 04:23
  • isnt the json cant support the null value? – Szemin Goh Oct 01 '14 at 04:27
  • There's something wrong with the MySQL query. See [this question/answer](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Chad Hedgcock Oct 01 '14 at 06:26
  • so, which part of my mysql query have the problem? – Szemin Goh Oct 01 '14 at 06:45
  • I haven't touched PHP/MySQL in long enough to know, but if you follow the top answer, namely, `if($result === FALSE) {die(mysql_error());}` you're likely to see the exact hang-up with the query. – Chad Hedgcock Oct 01 '14 at 07:45