1

i have the following request ajax:

$("#btnSave").click(function()
{ 
    var Url = 'http://localhost/Projetos-Laravel/Sibcomweb/public/panel/client/save';

    var Dados = $('#FormClient').serialize();

    $.ajax({
             type:'POST',
             url:Url,
             dataType: 'JSON',
             data: Dados,
             success:function(data){
                  if($.isEmptyObject(data.error)){
                     alert(data.msg);
                  }else{
                     alert('have errors');
                    }
                },
                error:function(e){
                  alert('Error !');
                  log.console(e);

                },
            });
        });

error log.console(e)

POST http://localhost/Projetos-Laravel/Sibcomweb/public/panel/client/save 500 (Internal Server Error)
send @ jquery.js:9566
ajax @ jquery.js:9173
(anonymous) @ create:431
dispatch @ jquery.js:5206
elemData.handle @ jquery.js:5014
create:446 Uncaught ReferenceError: log is not defined
    at Object.error (create:446)
    at fire (jquery.js:3317)
    at Object.fireWith [as rejectWith] (jquery.js:3447)
    at done (jquery.js:9274)
    at XMLHttpRequest.<anonymous> (jquery.js:9514)

the request ajax go to the controller but have some error in method of validation ...

I think the problem is in the validator method, am I doing something wrong?

public function store(Request $request)
{
    $dataForm = $request->all();

    $rules =[
    'name'=>'required|min:3|max:100',
    'number'=>'required|numeric',
    ];

    $valida = validator($dataForm, rules);
    if($valida->fails()) 
    {
        return $valida;
    }
    else
        return 'Ok';
}

how i do for return the var valida in type json?

  • Switch to the network panel in your browsers inspector, select the entry for the ajax call, and see what error laravel returns. Or, have a look in the laravel.log or your webservers log file. The PHP crashes for some reason, and you need to figure out that reason exactly. Only way of doing that is reading the error it returns. – Ole Haugset Jan 03 '18 at 11:50
  • The other part is however: Uncaught ReferenceError: log is not defined. The correct syntax would be console.log(e);, not log.console(e); – Ole Haugset Jan 03 '18 at 11:53
  • in network preview of navegation show this 'Object of class Illuminate\Validation\Validator could not be converted to string' – Bruno Aparecido da Silva Jan 03 '18 at 11:54
  • And if you google exactly that, you're one step further to figuring it out: https://stackoverflow.com/questions/37260036/object-of-class-illuminate-validation-validator-could-not-be-converted-to-string – Ole Haugset Jan 03 '18 at 11:54
  • i set the console.log, now the error is '500 (Internal Server Error)' – Bruno Aparecido da Silva Jan 03 '18 at 11:59
  • 1
    Yes, thats because your PHP script crashes. Due to the error you posted above. That error isn't returned since the data doesn't validate, its an actual PHP error. Meaning the script can't run until you fix your code. See my comment above. Google how to fix your 'Object of class Illuminate\Validation\Validator could not be converted to string' error, or follow the link in my comment to the SO-post on it. – Ole Haugset Jan 03 '18 at 12:01
  • the problem it was in hour of return, thanks for help haha – Bruno Aparecido da Silva Jan 03 '18 at 12:04

3 Answers3

3

log is not a fuction. Change log.console to console.log

Wim Pruiksma
  • 588
  • 5
  • 19
0

To see the response error:

Change your callback function on fail to:

  error: function(e){
    var errors = e.responseJSON;
    alert(errors);
    // Render the errors with js ...
  }
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
0

have two problems. first it was: log.console(e), but is console.log(e). and the second problem is in controller, the method of return not is correct.

that way it's correct:

public function store(Request $request)
{
    $dataForm = $request->all();

    $rules =[
    'name'=>'required|min:3|max:100',
    'number'=>'required|numeric',
    ];

    $valida = validator($dataForm, $rules);

    return response()->json(['error'=>$valida->errors()->all()]);
}