无法从jQuery ajax调用中获取json数据

时间:2021-09-10 08:21:48

I'm trying to get data from data.php via jQuery ajax call.

我正试图通过jQuery ajax调用从data.php获取数据。

My code looks like this:

我的代码如下所示:

var jsonData;

$.ajax({
        url: 'data.php',
        success: function(response) {
            jsonData = response;
        }
});

My data.php file is returning json formatted data but some text is in Unicode format. I set charset on data.php and on my javascript file, but still cant access responced data objects.

我的data.php文件返回json格式的数据,但有些文本是Unicode格式。我在data.php和我的javascript文件上设置了charset,但仍然无法访问响应的数据对象。

Any ideas?

有任何想法吗?

5 个解决方案

#1


14  

Try to put dataType: 'json' in you ajax call:

尝试将dataType:'json'放在你的ajax调用中:

var jsonData;

$.ajax({
        url: 'data.php',
        dataType: 'json',
        success: function(response) {
            jsonData = response;
        }
});

#2


2  

Also you can use this mechanism:

您也可以使用此机制:

$.getJSON( "data.php", function( response ) {
    jsonData = response;
});

It is more clean if you want get only JSON :)

如果你想只获得JSON,它会更干净:)

#3


1  

You should be using header() function in your PHP to set the proper response header (content type and charset):

您应该在PHP中使用header()函数来设置正确的响应头(内容类型和字符集):

header('Content-type: application/json; charset=UTF-8');

You should also repeat this at the top of HTML pages:

您还应该在HTML页面的顶部重复此操作:

<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />

See also:

也可以看看:

PHP UTF-8 cheatsheet

PHP UTF-8备忘单

#4


1  

PHP

PHP

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->query('SET NAMES utf8;');
    $stmt = $dbh->prepare($sql);  
    //$stmt->bindParam("id", $_GET[id]);
    $stmt->execute();

    $advice = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"items":'. json_encode($advice) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

Ajax

阿贾克斯

 var temp;
    $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: serviceurl,
            data: "{'userName':'" + userName + "' , 'password': '" + password                                   
                   + "'}",
            dataType: "json",
            success: function(msg) {
                            temp = jQuery.parseJSON(msg.d);
                          },
            error: function(xhr, ajaxOptions, thrownError) {}

        });

#5


0  

data.php

data.php

header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json'); 

and

$.ajax({
        url: 'data.php',
        dataType='json',
        success: function(response) {
            jsonData = response;
        }
});

#1


14  

Try to put dataType: 'json' in you ajax call:

尝试将dataType:'json'放在你的ajax调用中:

var jsonData;

$.ajax({
        url: 'data.php',
        dataType: 'json',
        success: function(response) {
            jsonData = response;
        }
});

#2


2  

Also you can use this mechanism:

您也可以使用此机制:

$.getJSON( "data.php", function( response ) {
    jsonData = response;
});

It is more clean if you want get only JSON :)

如果你想只获得JSON,它会更干净:)

#3


1  

You should be using header() function in your PHP to set the proper response header (content type and charset):

您应该在PHP中使用header()函数来设置正确的响应头(内容类型和字符集):

header('Content-type: application/json; charset=UTF-8');

You should also repeat this at the top of HTML pages:

您还应该在HTML页面的顶部重复此操作:

<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />

See also:

也可以看看:

PHP UTF-8 cheatsheet

PHP UTF-8备忘单

#4


1  

PHP

PHP

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->query('SET NAMES utf8;');
    $stmt = $dbh->prepare($sql);  
    //$stmt->bindParam("id", $_GET[id]);
    $stmt->execute();

    $advice = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"items":'. json_encode($advice) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

Ajax

阿贾克斯

 var temp;
    $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: serviceurl,
            data: "{'userName':'" + userName + "' , 'password': '" + password                                   
                   + "'}",
            dataType: "json",
            success: function(msg) {
                            temp = jQuery.parseJSON(msg.d);
                          },
            error: function(xhr, ajaxOptions, thrownError) {}

        });

#5


0  

data.php

data.php

header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json'); 

and

$.ajax({
        url: 'data.php',
        dataType='json',
        success: function(response) {
            jsonData = response;
        }
});