mercredi 24 février 2016

Laravel 5.2 - Return view from controller after ajax request

I have a javascript function that sends to a Controller some information (mostly vars like arrays and ids) to be inserted in a table, the problem is after the insertion is completed I want to return to a different view with a data array and i cant seem to do this (I think its because of the ajax request)

Javascript Code

  $('#importar').submit(function(e) {
       e.preventDefault();
           fdata=preparePostData();
        $.ajax({
            type:'POST',
            url:   $(this).prop('action'), // url, from form
            data:fdata,
            processData: false,
            contentType: false,
            success:function(data) {
                window.location.replace(data.url);
            }
        });
    }); // end form.submit

Function Prepare PostData()

 var file_data=$('input:file')[0].files;
        var postdata=new FormData();
        postdata.append('_token',token);
        postdata.append('startFrom',startFrom);
        postdata.append('idList',idList);
        postdata.append('nomeCampos',nomeCampos);
        postdata.append('posicaoCampos',posicaoCampos);
        postdata.append('file',file_data[0]);
        return postdata;

Controller Expected Code Do all inserts and functions and in the end

  $data = array('listNome' => $listName, 'contacts' => $contacts, 'errors' => $erro);
        return view("XPTO", $data);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire