Jquery mobile从.php文件获取json在本地工作但不在服务器上工作

i am working on a Jquery mobile project and want to load a json. The json comes from a .php file. The problem is that it will not load.

My .php file looks like this.

<?php
    $query=mysql_query("SELECT * FROM `data`");
    while($data=mysql_fetch_assoc($query))
    $output['result'] []=$data;
    header('Content-Type: application/json', true);
    echo json_encode($output);  
?>

I tried 2 different methods inside my jquery:

$.ajax({
    url: "http://www.example.com/jsonfile.php",
    dataType : "json"
}).success(function(data){

$.getJSON('http://www.example.com/jsonfile.php', function(data) {

Here is a little recap. When using a local .json file everything works. But when i call the same file on the server it does not. The error i get is:

The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.

In my .php file i can get rid of this error using: header('Content-Type: application/json; charset=US-utf-8');

Even without the error in my .php both files won't work. (my database is utf8_general_ci)

PHP white screen or no data is sign of an script error. Your ajax calls are correct, it's recomendable enable error reporting on development environment, so you need to check error logs or enable error_reporting to get the exception.

A few thing you can try:

  • hader('Content-Type: application/json;charset=UTF-8', true);
  • remove the Content-Type altogether
  • Call mysql_query("SET NAMES 'utf8'") after the connection to the db