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 have na problem. I'm trying to get some date from PHP file using $.ajax in JQuery

My JQuery code:

$.ajax({
url: "warcaby_s.php",
type: "POST",
data: { check: "1", id: id, player: player },
dataType: "json",
success: function(data)
{
    player_active = 1;
    timer = data.timeleft;
    pionki[0] = data.row_1.split("");
    pionki[1] = data.row_2.split("");
    pionki[2] = data.row_3.split("");
    pionki[3] = data.row_4.split("");
    pionki[4] = data.row_5.split("");
    pionki[5] = data.row_6.split("");
    pionki[6] = data.row_7.split("");
    pionki[7] = data.row_8.split("");

    $("#players").text("Grają: " + data.player_1 + " i " + data.player_2);

    rysuj_plansze(context);
}
});

My PHP file is:

    if ((isset($_REQUEST['check'])) && ($_REQUEST['check'] == 1))
{
    $zapytanie = 'select * from warcaby where id = '.$_REQUEST['id'];
    $wykonaj = mysqli_query($pol, $zapytanie);
    $wiersz = mysqli_fetch_array($wykonaj, MYSQLI_ASSOC);

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');

    echo json_encode(array("player_1"=>$wiersz['player_1'], "player_2"=>$wiersz['player_2'], "row_1"=>$wiersz['row_1'], "row_2"=>$wiersz['row_2'], "row_3"=>$wiersz['row_3'], "row_4"=>$wiersz['row_4'], "row_5"=>$wiersz['row_5'], "row_6"=>$wiersz['row_6'], "row_7"=>$wiersz['row_7'], "row_8"=>$wiersz['row_8'], "timeleft"=>$wiersz['timeleft']));
}

PHP output:

{"player_1":"mat","player_2":"mat","row_1":"02020202","row_2":"20202020",
"row_3":"00000000","row_4":"00000000","row_5":"00000000","row_6":"00000000",
"row_7":"01010101","row_8":"10101010","timeleft":27}

As for me - pure JSON

Firebug headers:

Nagłówki odpowiedzi

Cache-Control   no-cache, must-revalidate

Connection  Keep-Alive

Content-Length  207

Content-Type    application/json

Date    Tue, 25 Mar 2014 11:53:49 GMT

Expires Mon, 26 Jul 1997 05:00:00 GMT
Keep-Alive  timeout=5, max=58

Server  Apache/2.2.21 (Win32) PHP/5.3.10

X-Powered-By    PHP/5.3.10


Nagłówki żądania

Accept  application/json, text/javascript, */*; q=0.01

Accept-Encoding gzip, deflate

Accept-Language pl,en-US;q=0.7,en;q=0.3

Content-Length  22

Content-Type    application/x-www-form-urlencoded; charset=UTF-8

Host    localhost

Referer http://localhost/warc/warcaby1.php?start=1

User-Agent  Mozilla/5.0 (Windows NT 6.0; rv:28.0) Gecko/20100101 Firefox/28.0

X-Requested-With    XMLHttpRequest

Unfortunatly $.ajax.error function gives back parsererror

And I have no idea what I did wrong. Have You any suggestion what should i do to make it work?

share|improve this question
    
The content you provided is 201 characters long. But your header says 207. Is this the exact same request? If not please show it to us. –  Tobias Kun Mar 25 '14 at 12:03
    
I think it's some issue with "dataType" property. Try dataType: "application/json" –  0xC0DEGURU Mar 25 '14 at 12:09

2 Answers 2

As tobias-kun points out maybe you output buffer has let say "hidden chars", why don't you try using ob_clean function to prevent more output than you expect.

share|improve this answer
    
ob_clean() solve problem on my localhost Problem still existed on my host until I saved file without BOM - now everything works correctly Thank You very much –  user3457891 Mar 26 '14 at 9:21

try putting a

die;

just after executing

echo json_encode( ... 
share|improve this answer

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.