繁体   English   中英

从 PHP json 编码接收数据时出现 JSON 解析错误

[英]JSON parse error when receiving data from PHP json encode

我目前正在开发一个应用程序,该应用程序可以注入用户通过表单输入的链接、图像和其他内容。 按下提交按钮后,PHP 脚本会根据表单数据生成一个数组并将其编码为 JSON。 但是,使用 AJAX 调用 (jquery AJAX) 的接收 javascript 文件告诉我存在解析错误。 消息如下:

xrs.status = 200
thrown error = parsererror
xhr.statusText=OK

我已经使用 jsonlint 运行了由 PHP 编码的 JSON,我得到了有效的 JSON。 我问过我的朋友,似乎没有人知道问题是什么,所以我来了。 我希望你能帮忙。 下面是我正在使用的文件的代码。 顺便说一下,我正在使用会话,因为用户必须登录才能在表单中输入数据。 我不认为这会导致解析错误......

PHP:

header('Content-Type:text/json;charset=utf-8');
session_start();
if (isset($_POST["emagLinkRecomandareaZilei"])) {
$_SESSION["emagLinkRecomandareaZilei"] = $_POST["emagLinkRecomandareaZilei"];
} else {
echo "<p>eMAG product link not set</p>";
}

if (isset($_POST["emagDefaultImg"])) {
$_SESSION["emagDefaultImg"] = $_POST["emagDefaultImg"];
} else {
echo "<p>eMAG default img not set</p>";
}

if (isset($_POST["imgLinkRecomandareaZilei"])) {
$_SESSION["imgLinkRecomandareaZilei"] = $_POST["imgLinkRecomandareaZilei"];
} else {
echo "<p>eMAG image link not set</p>";
}

if (isset($_POST["specsTitle1RecomandareaZilei"])) {
$_SESSION["specsTitle1RecomandareaZilei"] = $_POST["specsTitle1RecomandareaZilei"];
} else {
echo "<p>eMAG specs title 1 not set</p>";
}

if (isset($_POST["specsTitle2RecomandareaZilei"])) {
$_SESSION["specsTitle2RecomandareaZilei"] = $_POST["specsTitle2RecomandareaZilei"];
} else {
echo "<p>eMAG specs title 2 not set</p>";
}

if (isset($_POST["specsTitle3RecomandareaZilei"])) {
$_SESSION["specsTitle3RecomandareaZilei"] = $_POST["specsTitle3RecomandareaZilei"];
} else {
echo "<p>eMAG specs title 3 not set</p>";
}

if (isset($_POST["specsDescription1RecomandareaZilei"])) {
$_SESSION["specsDescription1RecomandareaZilei"] =      $_POST["specsDescription1RecomandareaZilei"];
} else {
echo "<p>eMAG description 1 not set</p>";
}

if (isset($_POST["specsDescription2RecomandareaZilei"])) {
$_SESSION["specsDescription2RecomandareaZilei"] =   $_POST["specsDescription2RecomandareaZilei"];
} else {
echo "<p>eMAG description 2 not set</p>";
}

if (isset($_POST["specsDescription3RecomandareaZilei"])) {
$_SESSION["specsDescription3RecomandareaZilei"] = $_POST["specsDescription3RecomandareaZilei"];
} else {
echo "<p>eMAG description 3 not set</p>";
}
$productDetailsTop = array(
"emagLink" => $_SESSION["emagLinkRecomandareaZilei"],
"emagDefaultImg" => $_SESSION["emagDefaultImg"],
"imgLink" => $_SESSION["imgLinkRecomandareaZilei"],
"specsTitle1" => $_SESSION["specsTitle1RecomandareaZilei"],
"specsTitle2" => $_SESSION["specsTitle2RecomandareaZilei"],
"specsTitle3" => $_SESSION["specsTitle3RecomandareaZilei"],
"descSpecs1" => $_SESSION["specsDescription1RecomandareaZilei"],
"descSpecs2" => $_SESSION["specsDescription2RecomandareaZilei"],
"descSpecs3" => $_SESSION["specsDescription3RecomandareaZilei"],
);

echo json_encode($productDetailsTop);
exit;

接收 PHP 的 json_encode 字符串的 JavaScript 文件具有以下 AJAX 调用:

$.ajax({
               url: urlForPHPFile,
               cache: 'false',
               method: 'GET',
               contentType: 'application/json',
               dataType: 'json',
               success: function(data){
                   imgProduct.attr('src', data.imgLink);
                   imgProduct.attr('alt', data.productTitle);
                   //titleProduct.text(data.productTitle);
                   //titleProduct.append(data.completeEmagLink);
                   linkProductEmag.attr('href', data.emagLink);
                   defaultImgEmag.attr('src', data.emagDefaultImg);

                    h4s.each(function(indexH4s){//get all the elements of the h4s array - indexH4s is used as the variable i in a for(int i=0;i<h4s.lenght;i++)
                    switch(indexH4s){
                        case 0:
                            $(this).text(data.specsTitle1);

                        break;
                        case 1:
                            $(this).text(data.specsTitle2);
                        break;
                        case 2:
                            $(this).text(data.specsTitle3);
                        break;
                        }
                    });

                    descProduct.each(function(indexDescProduct){//the same applies for the descProduct array, where indexDescProduct is used as an index
                       switch(indexDescProduct){
                           case 0:
                               $(this).text(data.descSpecs1).css('fontWeight','bold');

                           break;
                           case 1:
                               $(this).text(data.descSpecs2).css('fontWeight','bold');
                           break;
                           case 2:
                              $(this).text(data.descSpecs3).css('fontWeight','bold');
                           break;
                          } 
                      });

               },
               error: function(xhr, thrownError){
                 alert('xrs.status = ' + xhr.status + '\n' + 
                        'thrown error = ' + thrownError + '\n' +
                        'xhr.statusText = '  + xhr.statusText + '\n' 
                        );
               }
           });

还有一件非常有趣的事情发生了 - 我已经创建了一个 PHP 文件,我在其中手动填充数组,没有用户输入,没有会话等,并且它完美地工作......下面是代码:

$productDetailsTop = array(
"emagLink"=>"http://profitshare.emag.ro/click.php?ad_client=cd46de6acbcb391a744848cc31896fd0&add_id=225304&redirect=monitor-led-samsung-21-5-wide-full-hd-negru-lucios-s22b150n%2Fpd%2FENS1TBBBM%2F%3Fref%3Dnl%26emag_click_id%3D41d3bb84f4e1e68fb96afbbd346dff17%26ref%3Dafs",
"emagDefaultImg"=>"http://profitshare.emag.ro/link_track.php?ad_client=cd46de6acbcb391a744848cc31896fd0&add_id=225304' alt='' border='0' width='1' height='1' style='border:none !important; margin:0px !important;",
"imgLink"=>"http://s1.emagst.ro/poze/big/img231176_29052012135325_0.jpg",
//"productTitle"=>"Tableta Google Nexus 7 cu procesor NVIDIA® Tegra® 3 Quad-Core 1.2GHz, 7, 1GB DDR3, 16GB, Wi-Fi, Android 4.1, Brown",
"specsTitle1"=>"Display",
"specsTitle2"=>"Facilitati",
"specsTitle3"=>"Caracteristici generale",
"descSpecs1"=>"3D:Nu, Tip display:LED, Diagonala:21.5 inch, Timp de raspuns:5 ms, Rezolutie optima:1920 x 1080, Tip ecran:Wide, Reproducere culori:16.7 milioane, Contrast tipic:600:1, Contrast dinamic:Mega DCR, Iluminare:200 cd/mp, Aspect imagine:16:9, Standarde:EnergyStar 5.0",
"descSpecs2"=>"Facilitati - Tuner TV:Nu, Boxe integrate: Nu",
"descSpecs3"=>"Putere consumata:30 W, Customized Key, MagicAngle, Eco Saving, Multi Screen S/W, MagicTune, MagicBright3, MagicColor",
"completeEmagLink"=>"<a href='http://profitshare.emag.ro/click.php?ad_client=cd46de6acbcb391a744848cc31896fd0&add_id=225304&redirect=monitor-led-samsung-21-5-wide-full-hd-negru-lucios-s22b150n%2Fpd%2FENS1TBBBM%2F%3Fref%3Dnl%26emag_click_id%3D41d3bb84f4e1e68fb96afbbd346dff17%26ref%3Dafs'>Monitor LED Samsung 21.5&quot;, Wide, Full HD, Negru Lucios, S22B150N</a><img src='http://profitshare.emag.ro/link_track.php?ad_client=cd46de6acbcb391a744848cc31896fd0&add_id=225304' alt='' border='0' width='1' height='1' style='border:none !important; margin:0px !important;' />"
);
header('Content-Type:text/json');
echo json_encode($productDetailsTop);
exit;

我比较了两个 PHP 文件的 json_encode 输出,jsonlint 为我提供了它们两个的有效 JSON。 我已经尝试了我能想到的一切,现在已经被这个问题困住了 2 天......如果有人知道发生了什么,请帮忙。 提前致谢!

JQuery ajax 脚本:

method: 'GET' <=== you should change the method by ==> method: 'POST' 

PHP 服务器响应。

if (isset($_POST <==== This is the error, choose which change ====>if (isset($_GET

暂无
暂无

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

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