Re: Enviando objeto javascript FormData via ajax

350 views
Skip to first unread message

Carlos Alberto Junior Spohr Poletto

unread,
Apr 3, 2013, 2:51:39 PM4/3/13
to caelum-...@googlegroups.com
Você pode simplificar isso:

var params = $("#seu-form").serialize();
var uri = "/bla/foo/";

$.post(uri, params, function(data){
   console.log(data.umResultJSONMaroto);
}, "json");



On Wed, Apr 3, 2013 at 2:26 PM, ADILSON TESSARI <adilson...@gmail.com> wrote:
Pessoal.
Estou tendo dificuldades para enviar um formdata via ajax. O request acontece, mas o campos chegam no controller todos nulos.
Segue código:

Código Javascript:
...
var formData  = new FormData();
        formData.append('doc_arquivo', file);
        formData.append('documento.nome',documento_nome);
        formData.append('documento.numero',documento_numero);
        formData.append('documento.tipoDocumento.id', documento_tipo);       
        formData.append('modelo.id', modelo_id);
        formData.append('paginaRet',pagina_retorno);
        formData.append('chaveRet',chave_retorno);
        formData.append('tipoCriacao', 'tipoArquivo');
       
       
        $.ajax({
            url: '../documento/uploadDocumentoAjax',
            data: formData,
            cache: false,
            processData: false,
            type: 'POST',
            contentType: 'multipart/form-data',           
            xhr: function() {
                myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
                }
                return myXhr;
            },           
            beforeSend: function (xhr){               
            },
            success: function(pagina){
            },
            error: function(xhr, status, error)
            {                   
            },
            complete: function(){               
            }

        });
...

Código java Controller:

    public void uploadDocumentoAjax(Documento documento, UploadedFile doc_arquivo ,String tipoCriacao, Modelo modelo, String pesquisa, String paginaRet, String chaveRet) throws Exception {
        if (modelo.getId() != null){
            documento.setModelo(modelo);
        }       
        documentoBo.salvar(documento, paginaRet, chaveRet, doc_arquivo, tipoCriacao, request.getRemoteAddr(), modelo);
        result.use(json()).withoutRoot().from(documento).include("documento").serialize();
    }   

Debug vraptor:
14:21:12,619 INFO  [STDOUT] 586500 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler  - Invoking interceptor ResourceLookupInterceptor
14:21:12,619 INFO  [STDOUT] 586500 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.http.DefaultResourceTranslator  - trying to access /documento/uploadDocumentoAjax
14:21:12,620 INFO  [STDOUT] 586501 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.http.DefaultResourceTranslator  - found resource [DefaultResourceMethod: DocumentoController.uploadDocumentoAjaxDocumentoController.uploadDocumentoAjax(Documento, UploadedFile, String, Modelo, String, String, String)]
14:21:12,623 INFO  [STDOUT] 586504 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler  - Invoking interceptor FlashInterceptor
14:21:12,660 INFO  [STDOUT] 586541 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.core.LazyInterceptorHandler  - Invoking interceptor ParametersInstantiatorInterceptor
14:21:12,660 INFO  [STDOUT] 586541 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.core.JstlLocalization  - couldn't find message bundle, creating an empty one
14:21:12,660 INFO  [STDOUT] 586541 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.http.ParanamerNameProvider  - Found parameter names with paranamer for DocumentoController.uploadDocumentoAjax(Documento, UploadedFile, String, Modelo, String, String, String) as [documento, doc_arquivo, tipoCriacao, modelo, pesquisa, paginaRet, chaveRet]
14:21:12,660 INFO  [STDOUT] 586541 [http-0.0.0.0-8080-5] DEBUG br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor  - Parameter values for [DefaultResourceMethod: DocumentoController.uploadDocumentoAjaxDocumentoController.uploadDocumentoAjax(Documento, UploadedFile, String, Modelo, String, String, String)] are [null, null, null, null, null, null, null]

Servidor de aplicação JBOSS 5.1.0
Vraptor 3.4.1

--
You received this message because you are subscribed to the Google Groups "caelum-vraptor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caelum-vrapto...@googlegroups.com.
To post to this group, send email to caelum-...@googlegroups.com.
Visit this group at http://groups.google.com/group/caelum-vraptor?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Atenciosamente,
Carlos Alberto Junior Spohr Poletto

Lucas Cavalcanti

unread,
Apr 3, 2013, 2:59:22 PM4/3/13
to caelum-...@googlegroups.com
Se é um upload vc não vai conseguir fazer via ajax assim...

tente usar algum plugin do jquery, tipo o uploadify pra fazer isso.
Reply all
Reply to author
Forward
0 new messages