简体   繁体   English

通过 Ajax 解析 PHP 文件获取的 JSON 数据时出现问题

[英]Problem parsing through Ajax a JSON data fetched by PHP file

I have difficulty parsing JSON date from my PHP file我很难从我的 PHP 文件中解析 JSON 日期

{"date":"20\/12\/2022","result":"£13000.00","medias":"BBC","country":"UK"}

but when I try to parse it and to see the data in the console.log - it's empty但是当我尝试解析它并查看 console.log 中的数据时 - 它是空的

Please help请帮忙

My Ajax Function我的 Ajax 函数

function ajax_call(){
    const style2 = $("#style1").val();
    const radio_btn = $('input[name="drone"]:checked').val();
    if(style2==""){
        document.getElementById("error").innerHTML = "Error: Please enter style code !"; 
        return false;
    } 
    {
        $.ajax({ 
            type: 'post',
            url: "t.php",  
            data: { styles: style2 , country: radio_btn},
            dataType: "json",
            success: function(data){  
                var jsondata = $.parseJSON(data);
                console.log(jsondata);
            }
        })
    }
}

My PHP我的 PHP

<php

header('Content-type: application/json');
$date = "20/12/2020";
$end_result = "£13000.00";
$medias = "BBC";
$country = "UK";

$sortjson = array('date' => $date,  
                    'result' =>iconv('Windows-1252', 'UTF-8', $end_result), 
                    'medias' => $medias, 
                    'country' => $country
            );

echo json_encode($sortjson, JSON_UNESCAPED_UNICODE);
?>

I believe there is a typo in the PHP code, you need to change the first line from:我相信 PHP 代码中有一个错字,您需要将第一行从:

<php

to

<?php

If the first line is <php , the output won't be valid JSON.如果第一行是<php ,则输出将不是有效的 JSON。

Complete PHP code:完整的PHP代码:

<?php

header('Content-type: application/json');
$date = "20/12/2020";
$end_result = "£13000.00";
$medias = "BBC";
$country = "UK";

$sortjson = array('date' => $date,  'result' =>iconv('Windows-1252', 'UTF-8', $end_result), 'medias' => $medias, 'country' => $country);


echo json_encode($sortjson, JSON_UNESCAPED_UNICODE);

?>

Also, I'm not 100% sure you need the iconv call, you could try the below code:另外,我不是 100% 确定您需要 iconv 调用,您可以尝试以下代码:

<?php

header('Content-type: application/json');
$date = "20/12/2020";
$end_result = "£13000.00";
$medias = "BBC";
$country = "UK";

$sortjson = array('date' => $date,  'result' => $end_result, 'medias' => $medias, 'country' => $country);

echo json_encode($sortjson, JSON_UNESCAPED_UNICODE);

?>

You have a error in your Success method:您的Success方法有错误:

use JSON.parse() method instead of $.parseJSON()使用JSON.parse()方法代替$.parseJSON()

success: function(data){  

   var jsondata = JSON.parse(data);
   console.log(jsondata);
}

You have to use JSON.parse()你必须使用 JSON.parse()

function ajax_call(){
const style2 = $("#style1").val();
const radio_btn = $('input[name="drone"]:checked').val();
if(style2==""){document.getElementById("error").innerHTML = "Error: Please enter style code !"; return false;} {
$.ajax({ 
    type: 'post',
    url: "t.php",  
    data: { styles: style2 , country: radio_btn},
                  dataType: "json",
                 success: function(data){  
   var jsondata = JSON.parse(data);
        console.log(jsondata);
}
})
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM