Welcome Guest, Not a member yet? Register   Sign In
jQuery Ajax and getting the data
#1

So I'm building game with HTML5 Canvas and I want to use CodeIgniter for my high score server.  Basically when the game has ended, you're prompted for your name and the game sends your score and name via ajax where it's updated in the db.

this is my controller:

PHP Code:
       public function addScore()
 
       {
 
           $this->load->library('form_validation');
 
           $this->form_validation->set_rules('name''required|min_length[3]|max_length[20]');
 
           $this->form_validation->set_rules('score''required|numeric');
 
           
            $name 
$this->input->post('name');
 
           $score $this->input->post('score');
 
           
            
//$resp['name'] = $name;
 
           //$resp['score'] = $score;
 
           if ($this->form_validation->run() !== FALSE
 
           {
 
               $this->highscore->new_score($name$score);
 
               $resp['success'] = true;

 
           } else
 
           {
 
               $resp['success'] = false;
 
           }
 
           
            $resp
['name'] = $name;
 
           $resp['score'] = $score;
 
           
            echo json_encode
($respJSON_UNESCAPED_UNICODE);
 
           
        


and this is my ajax call:
Code:
function addScore() {
 newScore = {
   name: '"' + $('#name').val() + '"',
   score: '"' + Player.worldPos[1] + '"'
 };
    $.ajax({
     url: "myGame/addScore/",
     type: 'post',
     contentType: 'application/json',
     data: newScore,
     success: function(data, status, xhr) {
       console.log("connection success"); // for debug, remove for release
       console.log(data);
       console.log(data.success);
     },
     error: function(xhr, desc, err) {
       console.log(xhr); // for debug, remove for release
       console.log("Details: " + desc + "\nError:" + err); // for debug, remove for release
     }
   }); //end ajax
}

My problem is that I can't get the ajax data from my controller.  $this->input->post('name') is null.  The data is there when I send it out (I console.log'd to make sure I wasn't sending null values).

this is the log:

Code:
connection success
{"success":false,"name":null,"score":null}
undefined


So how am I supposed to get the post data in my controller?  I've been pulling my hair out all night... Thanks in advance for any help
Reply


Messages In This Thread
jQuery Ajax and getting the data - by robdoesweb - 01-11-2016, 10:30 PM
RE: jQuery Ajax and getting the data - by keulu - 01-12-2016, 04:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB