jappoz92
jappoz92

Reputation: 33

ajax method's post not working

I've a problem I can't understand with my code. In my project I've an external js file containing this function below:

function setFeedback(voto, id) {
var str = '{"voto":'+voto+',"idcorso":'+id+'}';
var obj = JSON.parse(str);
 $.ajax({
 url: 'feed.php',
 method: 'POST',
 data: obj,
 contentType: "application/json; charset=utf-8",
 dataType: 'json',
 success: function() {   
   $( "#vota" ).hide( "slow", function() {
       alert( "Grazie per aver votato il corso" + voto + id );
   });
},
error: function(){
     alert("Chiamata fallita, si prega di riprovare...");   
}
});

}

that should send by post 'voto' and 'id'. My feed.php file is very simple:

<?php
session_start();
require_once('lib/DBhelper');
require_once('lib/Course.php');
if(isset($_POST['voto']) and isset($_POST['idcorso'])){
 $voto = $_POST['voto'];   
 $course = new CourseManager($_POST['idcorso']);
 $course->rateCourse($voto, $_SESSION['autenticato']);
}

and rateCourse is a simple insert method in a db like:

insert into feedback set id= $id ......

so i've tried in many ways but the function always returns the error function. Only if I don't write 'dataType: 'json' it return succesfully but nothing is written in db. I can't understand where is the mistake, if json is wrong or whatever else...

Thanks for help and sorry for my awful english :)


I tried

function setFeedback(voto, id) {
  var obj = { 'voto': voto, 'idcorso': id };
   $.ajax({
    url: 'feed.php',
    method: 'POST',
    data: obj,
    dataType: 'json',
   success: function() {   
      $( "#vota" ).hide( "slow", function() {
          alert( "Grazie per aver votato il corso" + voto + id );
       });
     },
     error: function(){
        alert("Chiamata fallita, si prega di riprovare...");   
     }
    });

  }

and the php file

<?php
 session_start();
 echo 'va male';
 require_once('lib/DBhelper');
 require_once('lib/Course.php');
 $postdata = json_decode(file_get_contents("php://input"), true);
 $id = $postdata['idcorso'];
 $voto = $postdata['voto'];
 $course = new CourseManager($id);
  $course->rateCourse($voto, $_SESSION['autenticato']);
 }

while I use dataType = 'json' I get the error message so I don't think the problem is in POSTed data but in connection. If I don't write dataType the success function is called but nothing is written in db.

Upvotes: 0

Views: 91

Answers (1)

Mike Brant
Mike Brant

Reputation: 71394

Because you are using application/json content type header in your request, PHP does not populate the POSTed data into $_POST. This is only done for form-based content types. You must instead read from PHP raw input.

$json = file_get_contents('php://input');
$voto = json_decode($json);

It also make no sense doing this:

var str = '{"voto":'+voto+',"idcorso":'+id+'}';
var obj = JSON.parse(str);

You can simplify by creating JS object directly:

var obj = { 'voto': voto, 'idcorso': id };

Any time you find yourself manually building JSON strings, you should ask yourself if you should really be doing that. JSON is a serialization format and should not be created manually in most cases.

Upvotes: 1

Related Questions