Http method post, date parameter is passing always empty (Angularjs + Laravel 5.2)

Asked

Viewed 870 times

1

I am using the Angularjs + Standard to build my application. The point is, I own $Cope.frequencias, which contains some information about a student’s attendance. I made an angular foreach to send this data to the database, however my data parameter (which contains this Scope) is always passing as empty.

Foreach:

angular.forEach($scope.frequencias, function(value, i, frequencias){
      $scope.frequencia = [];

      $scope.frequencia = frequencias[i];
      url = frequencias[i].url;
      
      //Aqui é mostrado o scope que quero enviar para o banco, está correto
      console.log($scope.frequencia);
      //aqui é mostrada a url para o metodo, que também está correta
      console.log(url);

      
      $http({
              method: 'POST',
              url: url,
              data: $.param($scope.frequencia),
              headers: {'Content-Type': 'application/x-www-form-urlencoded'}
              }).success(function(response) {
                  console.log(response);
              }).error(function(response) {
                  console.log(response);
                  alert('Um erro ocorreu. Check a log para mais detalhes.');
              }); 
              
    })

The controller of the Laravel, which receives the Scope and inserts it into the bank

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controler;
use App\Frequencia;
use Carbon\Carbon;

class Frequencias extends Controller
{
	public function salvar(Request $request)
	{
			
			$frequencia = new Frequencia;

	        $frequencia->data_frequencia = 
              $request->input('data_frequencia');
	        $frequencia->numero_faltas = 
              $request->input('numero_faltas');
			$frequencia->aluno_id = 
              $request->input('aluno_id');
			$frequencia->disciplina_id = 
              $request->input('disciplina_id');
		

	        $frequencia->save();
	        
	}

	public function update(Request $request)
	{
		
		$id = $request->input('id');

		
		$frequencia = Frequencia::find($id);

        $frequencia->data_frequencia = 
          $request-  >input('data_frequencia');
        $frequencia->numero_faltas = 
          $request->input('numero_faltas');
		$frequencia->aluno_id = 
          $request->input('aluno_id');
		$frequencia->disciplina_id = 
          $request->input('disciplina_id');
	

        $frequencia->save();
        
		
	}
}

The routes used by the Laravel

//rotas para frequencia

Route::post("/api/v1/frequencias/salvar","Frequencias@salvar");

Route::post("/api/v1/frequencias/atualizar","Frequencias@update");

When arriving at the controller, it tries to insert the parameters as null, and the error in the database, because some attributes are joined. I insert this way in several points of my application, and only in the one that is giving problem. Thanks in advance for the help.

  • When you send post to the method you need to debug in the browser console, you have already done this...?

1 answer

2

Check that the parameter "data_frequencia" is actually being sent. As generally use Chrome, in Developer Tools -> Network la has its POST, ve se esta enviar algum valor.

And a hint, use the value variable instead of the array, for example.

 angular.forEach($scope.frequencias, function(frequencia, index){

    $http({
        method: 'POST',
        url: frequencia.url,
        data: $.param(frequencia),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(response) {
        console.log(response);
    }).error(function(response) {
        console.log(response);
        alert('Um erro ocorreu. Check a log para mais detalhes.');
    }); 

})
  • Nothing is being sent... not in your way, not in mine, I don’t know what to do

Browser other questions tagged

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