1

Here I've built a script to generate the following JSON output:

[{"lat":41.081348,"lon":14.73292,"type":"F"},...,{"lat":41.09837,"lon":14.83176,"type":"F"}]

However when I try to get data using the following Javascript code:

        $.ajax({
            url: 'myJSONscript.php',
            dataType: "json",
            success: function (data, status, xhr) {
                console.log("data:"+data);
                console.log("status:"+status);
                console.log("xhr:"+xhr);
                }, //End Success
            error: function () {
                alert("ERROR");
                }
            });

I get just an empty data variable
(in other words the console returns
data:empty,
status:success,
xhr:[object Object]).
The JSON output is valid (tested with JSONlint) and was built using the following code:

$arr=array();
while ($row=mysqli_fetch_array($r)) {
$element=array('lat'=>floatval($row['LATITUDE']),'lon'=>floatval($row['LONGITUDE']),'type'=>$row['TYPE']);
    array_push($arr, $element);
    }
$data=json_encode($arr,true);
header("Content-Type: application/json", true);
echo $data;

I cannot figure out what's wrong...

14
  • Have you inspected the request in the Network tab in browser console? Commented Apr 9, 2016 at 16:32
  • read the fabulous manual for json_encode ... your syntax is incorrect. Remove the true parameter. Commented Apr 9, 2016 at 16:32
  • 1
    put a ' at the end of myJSONscript.php Commented Apr 9, 2016 at 16:35
  • 1
    Fixed! the contentType: "application/json" cannot be used together with dataType: "json", removing contentType solves the problem. Commented Apr 9, 2016 at 17:13
  • 1
    @PowerEngineering : make this an answer and accept it. The question and the answer are useful to others ! Commented Apr 9, 2016 at 17:29

1 Answer 1

2

Fixed! the contentType: "application/json" cannot be used together with dataType: "json", removing contentType solved the problem.

Sign up to request clarification or add additional context in comments.

Comments

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.