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

时间:2012-12-12 10:45:30

标签: php ajax json

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

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

我使用jsonlint运行由PHP编码的JSON,我得到Valid 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天这个问题...如果有人对发生的事情有所了解,请帮忙。提前谢谢!

1 个答案:

答案 0 :(得分:0)

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