Home > Net >  laravel ajax not entering success function
laravel ajax not entering success function

Time:12-09

first let me share some code:

View / Form:

 <form id="xlsForm" enctype="multipart/form-data">
            @csrf
            <input  id="loadFile" type="file" name="excelfile" accept=".xls,.xlsx,.docx,.doc" required/>
            <input type ="hidden" name="load" value="0">
            <button   name ="loadSubmit" id="loadSubmit" type="submit" value ="Analyze">Analyze</button>
        </form

JQuery

 $('#xlsForm').submit(function uploadFile(event) {
        event.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
            }
        });

        $('#loader').show();
        $.ajax({
            url: "{{route('ExcelToArray')}}",
            method: 'POST',
            data: new FormData(this),
            dataType: 'JSON',
            contentType: false,
            cache: false,
            processData: false,
            success: function (response) {
                console.log("entered response")
                $("#inner").empty();
                $.each(response.like_values, function (index, value) {
                    $("#inner").append
                    ('<div >'  
                        '<div >'  
                        '<input  data-name="'   value   '" type="number" name="weight['   value   ']" min="1" max="3" value="1" style="text-align:center"> '  
                        '</div>'  
                        '<div   style="padding-left: 9px">'  
                        '<input type="checkbox"  data-name="'   value   '"  value="'   value   '" name="checkbox['   value   ']"  checked >  '  
                        '<label style="color:'   response.color[value]   '; font-weight: bold" for="skillChoice_'   index   '">'   value   ' </label>'  
                        '</div>'  
                        '</div>');
                });

            },
           error: function (jqXHR, textStatus, errorThrown) {
                    alert('jqXHR is : '   jqXHR.value
                           'textStatus is : '   textStatus
                           ' errorThrown is : '   errorThrown);

            },

          complete: function () {

              console.log("entered complete")
                $('#loader').hide();
                load_employees()

            },
        });


    });

In my controller there is a lot of stuff which i will not share because this code

  return response()->json([
         'like_values' => $like_values,
         'color' => $color,]);

at the end of my controller function is working and in chrome i can see the data available: like_values and color in my chrome

My problem is that my success function is not being called and the arrays are not being iterated. However it used to work but i don't know whats the problem now. I am getting an error (which i remember it used to work even with this error. I used to ignore it but it says :

SyntaxError: Unexpected token s in JSON at position 0

Maybe i am missing something related to this error.

Does anyone has a clue whats wrong?

Thanks in advance

CodePudding user response:

JSON Syntax Rules state that data should be separated by commas however you added an extra comma in your return keyword to end your controller function.

Change this:

  return response()->json([
     'like_values' => $like_values,
     'color' => $color,]);

To:

  return response()->json([
         'like_values' => $like_values,
         'color' => $color]);

CodePudding user response:

i've found the issue. If you see the response in the screenshot i made, you can see also the output : string(15) xlsx ausgelöst. So i was also doing a var_dump() in my controller. So this string was not in json format this is why it was never going to my success function. Actually i never knew that everything which i send from controller to my view will be evaluated by ajax. I thought its only this code.

 return response()->json([
     'like_values' => $like_values,
     'color' => $color]); 

which will be considered.

However after removing the var_dumps() from controller it worked fine, it was only my actual response data which were sent to the view and ajax recognized it as json and success: function was executed.

Thanks to yall anyway.

  • Related