I'm using Laravel Framework, and I'm trying to create an Ajax request, so I can send the Form data to the Controller. The thing is, I'm passing a string but in the controller, when I try JSON_decode it says that I'm giving it an array and not a string. What am I doing wrong?
View (ajax)
function getData() {
var name = $('#name').val();
var tin = $('#tin').val();
var password = $('#password').val();
var currency = $('#currency').val();
var data = {"name": name, 'tin': tin, 'password': password, 'currency': currency};
var obj = JSON.stringify({"name": name, 'tin': tin, 'password': password, 'currency': currency});
console.log(obj);
$.ajax({
type: "POST",
url: "teste",
data: obj,
success: function (result) {
alert(result);
}
});
}
Controller:
$data = $_POST;
//$string = json_encode($data);
$test = json_decode($data);
echo $test->name ;
//echo "data: $string, gravada com sucesso!";
die;
If someone could help me...
Just solved.
$.ajax({
type: "POST",
url: "teste",
data: {"name": name, 'tin': tin, 'password': password, 'currency': currency},
success: function (result) {
alert(result);
}
});
And in the controller, I encoded and then decoded..and it worked Thanks :)
json_decodetojson_encode