1

My first time writing, but not the first here. I felt forced to ask here because none of those apparently alike questions here were able to give me a clear workaround or solution. My projects involves a tic-tac-toe game, all jQuery, PHP and log file based. It's working fine, but for every information sent to the txt log, I have to star a new $.post to retrieve the information that will be displayed in different parts of the page, not all in a single list (specially O and X that are retrieved back to their original text element) ... The game is real-time, multiplayer and is working fine, but is using two txt logs written by PHP which also manages the game: one log keeps the position (filled space) and the other what char was used (X or O). Nice, but I'm calling the two via $.post, storing their data in respective global variables and using them in a setInterval, to update on time for the other player. But as there are many other informations like name and remaining moves (from 9 to 1), I'd like to put everything in a single log, something easier to be retrieved all in a time and be displayed in their respective positions through the game page. My problem (now what I need to get working for now) here is retrieving them from PHP. During tests, I simulate three informations: position, char (O or X) and player's name, put them in an array and echo them with json_encode, as it follows (my jQuery post and then my PHP):

<script type="text/javascript" src="jquery.js"></script>


$.post ("json.php", {pos:"pos",weapon:"weapon",nome:"nome"}, function(data){

alert (data); //returns null...
//alert (data.pos); //nothing, script ignores execution
//alert (data[0].pos); //returns null...
//alert (data[1].pos); //returns null...
//alert (data[2].pos); //returns null...
//alert (data[3].pos); //returns null...
//tests above based on some suggestions I'fe found through the web, ineffective...

},"json");

And my PHP:

<?php

//Simulating values being received during the game:

$pos='s3'; //simulation player's move on slot 3
$weapon='O'; //player puts O on slot 3
$name= 'fulano';

//Putting general player info into the array to be used by jQuery:

$coord= array();

$coord['pos']= $pos; //Sets the position where the player put his char
$coord['weapon']= $weapon; //The "weapon" to be put in respective "pos"
$coord['nome']=$name; //Player's name to be displayed during the game

echo json_encode($coord); //encoded json is returning {"pos":"s3","weapon":"O","nome":"fulano"} nicely
?>

Both are post, both using json, jQuery returns something, but is null, PHP echoes alright... $.ajax simply didn't make it either... What I need: that $.post retrieves every datum so that I can store one by one in their respective global variables, but if alert can't even show one of them coming, something's wrong. Changed every variable names to avoid any kind of conflict, not solved... I made this test just to make sure PHP json would be clearly gotten by jQuery, what's not happening here. My HTML is using default charset, I'm testing it in localhost, I have the main project working perfectly here, this was a side test to test specifically json response. I simply had no success in using $.ajax ({}) (my jQuery is 1.7.1), no matter what, the script simply stops executing, PHP keeps responding alright here, but jQuery is not giving me what it should other than "null", I've tested it on FF15 and IE8, problem is just with my json routine on jQuery, PHP is working fine and my $.posts here and on the main project, too. What could be going wrong here? Why is it simply returning data as null regardless to what I may try? I thank everyone in advance, I'm just asking because there were no examples here and outside that really gave me the answer, and 99,9% depend on Ajax, but Ibelieve it can be simpler like this.

6
  • have you checked for errors in firebug? use console.log output, it's easier to debug. Commented Sep 7, 2012 at 13:24
  • Hello, h4b0! Yes, I've been following the process with firebugs, the result is pretty much it "POST 127.0.0.1/json.php 200 OK 167ms", but when I checked the response header, I got this: "Content-Length 0 Content-Type text/html; charset=ISO-8859-1". As json works (AFAIK) only with utf-8, could it be the source of a null response? I'll try and fix it on PHP side and if I get any result, Ill publish here ;) Thanks for your response ;) Commented Sep 7, 2012 at 20:53
  • Ah, noticing that PHP is returning the results by itself without a json problem, only jQuery response seems to lose the retrieved information... Commented Sep 7, 2012 at 21:16
  • tried (server side): 'base64_encode'= no solution, htmlentities= no solution, utf8_encode()= no solution, utf8_decode ()= no solution... tried on client side (jQuery): $getJSON= no solution/same results (null) things are simply made to be complicaded... I'll try some hacks and IF I get results, I'll tell you how to avoid this hell... Commented Sep 7, 2012 at 22:42
  • tried on PHP: header('Content-Type: application/json');= no solution... Commented Sep 7, 2012 at 22:47

2 Answers 2

1

Add this to your code

$.post ("json.php", {pos:"pos",weapon:"weapon",nome:"nome"}, function(data){
     $(data).each(function(){
            alert(this.s3);
            alert(this.0);
            alert(this.fulano);
     });
},"json");
Sign up to request clarification or add additional context in comments.

Comments

0

Valid JSON is as follows: {"key":"value"} not {key:"value"}

Note: All quote encapsulated, not just the value. Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.