简体   繁体   中英

Getting JSON data from PHP file using Ajax and JQuery

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?

正如tobias-kun指出,也许您的输出缓冲区让我们说了“隐藏字符”,为什么不尝试使用ob_clean函数来防止超出预期的输出。

try putting a

die;

just after executing

echo json_encode( ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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