Jquery post with problems accessing a php class

Asked

Viewed 97 times

-1

I’m having trouble accessing a class via $.post from jquery. There is some internal error that is not shown in the console. If I instantiate the class via php, it works perfectly, just by jquery that does not return anything. I am using spl_autoload in index only.

Controllers/Funilcontroller.php

<?php

namespace Controllers;
use Models\Vendas;

class FunilController
{
  public function __construct()
  {
    $v = new Vendas;
    $vendas = $v->index();
    return $vendas
  }
}

Models/Sales.php

<?php

namespace Models;
use Models\Conexao;

class Vendas extends Conexao
{
  public function index()
  {
    return 'olá';
  }
}

index php.

<?php
spl_autoload_register(function( $class ) {require_once str_replace( '\\', DIRECTORY_SEPARATOR, $class ) . '.php';});
?>

(...)

<script>
$.post('Models/Vendas.php',
 {solicitar: true, tab:1, cpf: Cpf, data_ini: dataIni, data_fim: dataFim},
 function(data){
 $('#res1').empty().append(data);
}, 'json');
</script>

1 answer

0

Apparently the error is in contenttype, tries to set as example

  $.ajax({
   type: "POST",
   url: "/url",
   dataType : "json", // <--tipo de retorno esperado
   contentType: "application/json", // <-- tipo de dado enviado
   data: {
     solicitar: true, 
     tab:1, 
     cpf: Cpf,  
     data_ini: dataIni, 
     data_fim: dataFim},
   contentType: "application/json"
   success()
})

dataType (default: Intelligent Guess (xml, json, script, or html)) font: Ajax

Restructure the return of your Backend to an Object json {msg:"Hello"}

  • I don’t know why, but it’s not sending the data via post: $. ajax({ type: "POST", url: "Controllers/Jsoncontroller.php", data: { prompt: true, tab:4, Cpf: Cpf, data_ini: dataIni, data_end: dataFim ;contenttype: "application/json", beforeSend: Function(){ console.log('sending...'+Cpf); } }) . done(Function(msg){ console.log(msg); }) . fail(Function(jqXHR, textStatus, msg){ console.log(msg); });

  • Isn’t it coming in the backend? Or Voce isn’t having a comeback??

  • It’s coming, why I managed to return hello, just not receiving the data passed by post there.

  • Oh yes, dataType (string), I updated the code, see if it’s right... The backend is returning a "hello"??

  • Still not receiving the post, is returning the 'out': if(isset($_POST['request']) or isset($_POST['Cpf']) { echo json_encode('here'); } Else { echo json_encode('out'); }

  • I’m starting to think the problem isn’t in the post.

  • Do you usually use namespace in your projects? Why is it the first time I’m using it and is giving it away =/

  • @Glenysmitchell does so try the post, by Postman, then we will see where the error is...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.