Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently developing an application that injects links, images and other stuff that a user inputs through a form. After the submit button is pressed, a PHP script generates an array based on form data and encodes it as JSON. However, the recieving javascript file that uses an AJAX call (jquery AJAX) gives me that there is a PARSE ERROR. The message is as following:

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

I've run the JSON that is encoded by PHP using jsonlint and i get Valid JSON. I've asked my friends and it seems no one knows what the problem is, so here i am. I hope you can help. Below is the code for the files that i'm using. By the way, I'm using sessions as the user has to log in in order to input data in the form. I don't think that this would cause a parse error...

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;

The JavaScript file receiving PHP's json_encode string has the following AJAX call:

$.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' 
                        );
               }
           });

There is also a very intresting thing happening - i've created a PHP file in which i manually populate the array, no user input, no sessions etc and that one works perfectly... Below is the code:

$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;

I've compared the json_encode output for both PHP files, jsonlint gave me Valid JSON for both of them. I have tried everything i could think of, been stuck on this problem for 2 days now... If anyone has an idea about what is happening, please help. Thanks in advance!

share|improve this question
    
Your PHP sends text/json as content type while your js expects application/json, i don't think it should change anything, but have you tried changing one for the other? –  jaudette Dec 12 '12 at 10:59
    
@jaudette - thanks for the heads up - i've set the header's content type first as application/json, both in the php files and the js file. The php file in which i insert manually the data, it works with both text/json and application/json. When using the other PHP file that recieves user input, the error persists, no matter the content type - text/json or application/json... –  user1895657 Dec 12 '12 at 11:14
    
Have you tried more than one browser? because the doc mentions that the parseJSON call is browser specific if the browser supports it? –  jaudette Dec 12 '12 at 11:35
    
@jaudette - i'm testing with google chrome version 23.0.1271.95 m and firefox version 17.0.1 –  user1895657 Dec 12 '12 at 11:42
    
Have you tried to monitor what happens in firebug? (Network > XHR) –  steven Dec 12 '12 at 11:45
show 1 more comment

1 Answer

JQuery ajax script:

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

PHP Server response.

if (isset($_POST <==== This is the error, choose which change ====>if (isset($_GET
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.