Ajax post error

Published 3 weeks ago by david001

I am getting error on saving data with Ajax,but without ajax it works.

and at console it says : 500 (Internal Server Error)

At Network it gives
{,…}
exception
:
"Illuminate\Database\QueryException"
file
:
"C:\xampp\htdocs\WAN\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
line
:
664
message
:
"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null (SQL: insert into `events` (`title`, `description`, `start_date`, `end_date`, `address`, `feature_image`, `user_id`, `slug`, `updated_at`, `created_at`) values (maha shiva ratri, , nnnnnnnnnnnnnnn, nnnnnnnnnnnnnn, kathmandu, public/files/z4w39pf94ynWB1OMqaNTQktC90aIAtGmkXciiclq.jpeg, 1, maha-shiva-ratri, 2017-11-22 17:11:41, 2017-11-22 17:11:41))"
trace
:
[{file: "C:\xampp\htdocs\WAN\vendor\laravel\framework\src\Illuminate\Database\Connection.php",…},…]
0
:
{file: "C:\xampp\htdocs\WAN\vendor\laravel\framework\src\Illuminate\Database\Connection.php",…}
1
:
{file: "C:\xampp\htdocs\WAN\vendor\laravel\framework\src\Illuminat
Some basic Git commands are:

controller:

public function store(Request $request)
    {
        if($request->file('feature_image')){
            $uploadedFile = $request->file('feature_image');        
            $path = $uploadedFile->store('public/files');
        }else{
            $path = 'default.png';
        }
        $event = new Event;
        $event->title = $request->title; 
        $event->description = $request->description; 
     
        $event->feature_image = $path;
        $event->user_id = \Auth::user()->id;
        $event->slug = str_slug($request->title);
        $event->tags()->sync($request->tag);
        $event->save();
        return "ok";


    }

view:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <form action="#" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading"></div>

                <div class="panel-body">
                <div class="form-group">
                    <label>Title :</label>
                    <input type="text" name="title" class="form-control" id="title" >
                </div>
                 <div class="form-group">
                    <label>Describe :</label>
                    <textarea name="description" id="description"></textarea>
                </div>
      
                    <div class="form-group">
                        <label>Feature Image:</label>
                  
                    <input type="file" id="feature_image" name="feature_image" class="form-control " >
                   </div>

                   <div class="form-group">
                       <label>Tag:</label>
                    <select class="form-control event-tag" name="tag[]" multiple="multiple" id="tag">
                        <option value="1">Alabama</option>

                        <option value="2">Wyoming</option>
                        <option value="3">ABC</option>
                        <option value="4">DEF</option>
                    </select>
                   </div>


              

                 <div class="form-group">
                   
                    <input type="" name="" id="btnClick" value="PUBLISH" class="btn btn-primary pull-right">
                </div>




               </div>
            </div>
        </div>
    </form>
    </div>
</div>
@endsection

       ```      
js:
$("#btnClick").click(function(e){ e.preventDefault(); var title = $("#title").val(); var description = $("#description").val();
var tag = $("#tag").val();


var form = new FormData();
var feature_image = $('#feature_image')[0].files[0];
form.append('feature_image', feature_image);
form.append('title', title);
form.append('description', description);
form.append('tag', tag);


var route = "events/store";


  $.ajax({
   
    url: route,
    cache: false,
    processData: false,
    contentType: false,
    headers: {
  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                 },
    type: 'POST',
    
    data:form,

    beforeSend:function()
    {
      
    },
    
    success:function(data){
      console.log(data);
      alert('success');

    },
    error:function(msj){alert('Error!')}
        
   
});

});

``` route:

Route::post('/events/store', '[email protected]');

please help me to fix this issue

rumm.an

I suggest you to add validation in your store method. For now, add dd($request->description) in your controller and tell me your output.

nanadjei2

I will also suggest to try and make it work in laravel before you try it with ajax. With that you can have a better way of debugging most of the server side errors

david001

on dd($request->description) i got success from alert .but when i remove dd($request->description) i got same error. it may be because of image

rumm.an

I want to see the preview of resonse not you javascript actions. What response did you get from your ajax request when you use dd()

david001

i got nothing ,i should be description but i didn't got,my route is correct

zion
zion
3 weeks ago (20,940 XP)

Do you have 'description' in your $fillable array in the model?

rumm.an

you should have received an html respose, use your browser's dev tool's network tab and see your request preview there.

rumm.an

@zion good point! @david001 make sure you have description in the protected $fillable array in your model, or otherwise, you must have protected $guarded = [], means $guarded array should be empty.

david001

At network i got:

Sfdump = window.Sfdump || (function (doc) { var refStyle = doc.createElement('style'), rxEsc = /([.*+?^${}()|\[\]\/\\])/g, idRx = /\bsf-dump-\d+-ref[012]\w+\b/, keyHint = 0
rumm.an

What tool are you using something like Postman or some browser?

david001

chrome browser

rumm.an

Look at your xhr request in network tab click on your request and click prieview tab that will render the html response so that you can see whats coming in $request

Please sign in or create an account to participate in this conversation.