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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19475991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:03:35  来源:igfitidea点击:

Can't get json data from jQuery ajax call

javascriptjqueryajaxjson

提问by zur4ik

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

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

My code looks like this:

我的代码如下所示:

var jsonData;

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

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

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

Any ideas?

有任何想法吗?

回答by zur4ik

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;
        }
});

回答by Max

Also you can use this mechanism:

您也可以使用此机制:

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

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

如果您只想获取 JSON,它会更干净 :)

回答by Ahsan Shah

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

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

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 备忘单

回答by ridvanzoro

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) {}

        });

回答by jycr753

data.php

数据.php

header('Content-type: application/json'); 

and

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