0

I am having issues retrieving data from PHP by using Ajax. I am stuck and have been spending lots of time trying to find out where the problem is.

Here is my php code:

        <?php //ajax/default_chart_numbers.php
        require_once '../core/db_connection.php';
        $lotto = new Lotto();
        $ultimo_concurso=$lotto->ultimo_concurso('foo');
        $ultimos_numeros_m=$lotto->ultimos_numeros('bar');

        $R1m=$ultimos_numeros_m[1];
        $R2m=$ultimos_numeros_m[2];
        $R3m=$ultimos_numeros_m[3];
        $R4m=$ultimos_numeros_m[4];
        $R5m=$ultimos_numeros_m[5];
        $R6m=$ultimos_numeros_m[6];
        $R7m=$ultimos_numeros_m[7];

        //preparing json
  $json=array('y'=>$ultimo_concurso,'n1'=>$R1m,'n2'=>$R2m,'n3'=>$R3m,'n4'=>$R4m,'n5'=>$R5m,'n6'=>$R6m);
        print json_encode($json,true);
        ?>

The output of the PHP file is:

{"y":"2745","n1":"1","n2":"13","n3":"19","n4":"29","n5":"41","n6":"46"}

And here is the jQuery code:

<script>
$(document).ready(function(){
    /*Retriving data from PHP file*/
    $.ajax({
        url: "ajax/default_chart_numbers.php",
        cache: false, 
        dataType: "json",
        timeout:3000,
        success : function (response, textS, xhr) {
            alert("everything ok :)");
        },
        error : function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Error " + errorThrown);
             if(textStatus==='timeout')
              alert("request timed out");
        },
        complete: function(data){
            y=data.y;
            alert('The id number is '+ y);
        }
    });
});
</script>

When executing, the value is undefined. I mean, the alert i get is The id number is undefined.

What am i missing?

2
  • if you open just php file url what is the output ? Commented Mar 30, 2014 at 16:41
  • You must be getting an alert from either the error or the success handler as well? Commented Mar 30, 2014 at 16:44

3 Answers 3

2

There's no true in json_encode, there is in json_decode to get an array, but now you're creating a string

change

print json_encode($json,true);

to

echo json_encode($json);

and the complete handler doesn't get the data, it has two arguments, the XHR object and the statuscode, the success handler gets the data

$.ajax({
    url: "ajax/default_chart_numbers.php",
    cache: false, 
    dataType: "json",
    timeout:3000,
    success : function (data) {
        y=data.y;
        alert('The id number is '+ y);
    },
    error : function (xmlHttpRequest, textStatus, errorThrown) {
         alert("Error " + errorThrown);
         if(textStatus==='timeout')
             alert("request timed out");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

On PHP side, send the JSON with:

header('Content-Type: application/json');
echo json_encode($json);

Maybe log the incoming data to the console:

  1. inside success: add an console.log(response)
  2. inside complete: add an console.log(data.y)

Comments

-1

This is missing: https://api.jquery.com/jQuery.parseJSON/

try using:

 result = $.parseJSON (data);

result.y has your value

2 Comments

No it's not, the dataType is set to JSON, so it's automatically parsed, parsing it again will lead to errors.
@adeneo Ah, yes I overlooked that one. Well said.

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.