2

i'am lost here, i'm working on a game which will need to make a call to the server to inisiate the game and register the user and get some game data. but upon making the call to start game nothing happens and i keep getting this error message. i have already validated the json response i get from the server. below is my code please help :(

JSON

{
    "message": "Game started",
    "code": 200,
    "status": "success",
    "user_session_id": "217c7a4759388ae0af80bdf7668e4bf5",
    "points": 0,
    "dataBN4": {
        "1": {
            "number_id": 181,
            "guess_number": 183
        },
        "2": {
            "number_id": 182,
            "guess_number": 968
        },
        "3": {
            "number_id": 183,
            "guess_number": 742
        },
        "4": {
            "number_id": 184,
            "guess_number": 986
        },
        "5": {
            "number_id": 185,
            "guess_number": 230
        },
        "6": {
            "number_id": 186,
            "guess_number": 580
        },
        "7": {
            "number_id": 187,
            "guess_number": 648
        },
        "8": {
            "number_id": 188,
            "guess_number": 691
        },
        "9": {
            "number_id": 189,
            "guess_number": 424
        },
        "10": {
            "number_id": 190,
            "guess_number": 85
        }
    }
}

Jquery:

function startNewGame() {
    $('#game').html(activeHtml);
    n_url = gameBaseUrl + 'action=start_game';
    $.ajax({
        dataType: "JSON",
        url: n_url,
        success: function(data){
            userSessionId = data.user_session_id;
            answersObject = data.dataBN4;
        }

    }).done(function(){
        resortAnswers();
        createAnswers();
    });
    /*
    $.getJSON(url, {}, function (data) {
        userSessionId = data.user_session_id;
        answersObject = data.dataBN4;
    }).done(function () {
        resortAnswers();
        createAnswers();
    });
    */
}

PHP generates json;

<?php
ob_start();
        //make db entries and loges to data array
        $data['message'] = 'game started';
        $data['status'] = 'success';
        $data['code'] = 200;
        $data['points'] = 0;
        $data['dataBN4'] = [
        ['number_id' => 11, 'guess_number' => 33],
        //and so for until i added 9 more similar arrays here
        ];
    header('Content-Type: application/json');
    echo (json_encode($data));

$out = ob_get_clean();
echo $out;

UPdate:

i installed fire bug and now it show this error: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://ourserverblabla/index.php?action=start_game. (Reason: CORS header 'Access-Control-Allow-Origin' missing)"

20
  • how is the JSON generated? Commented Jan 21, 2016 at 0:48
  • 3
    Pretty sure you have a BOM at the 1st character position Commented Jan 21, 2016 at 0:50
  • inspect actual request itself in browser dev tools to see what is being returned Commented Jan 21, 2016 at 0:55
  • @DanielA.White with the php code above :) Commented Jan 21, 2016 at 0:56
  • 1
    Set the Content-Type header to application/json in your PHP script. header('Content-Type: application/json');. Potentially also use jQuery.parseJSON() on the jQuery side to ensure it's handled correctly. Commented Jan 21, 2016 at 1:15

1 Answer 1

2

FINALLYY it worked.

i added this line at the top of the script, and it worked.

header('Access-Control-Allow-Origin: *');

it seems like this is an issue with jquery and some browsers. mainly browsers as they will block some of the communication for reason i dont even know.

Sign up to request clarification or add additional context in comments.

2 Comments

It's because, you have cross-domain communications, which is blocked by default from your server (not browser). By using this header, you are allowing to any domain to send AJAX calls to your domain.
@TaReQMahMooD thanks that explains alot, reading about this now :)

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.