Question

I've been trying to submit a file with ajax $.post.

var json_data = update_json_({name:first_name+' '+last_name,email:email,email2:email2,tele:tele,message:message,cc:$("#cc<?php echo $rand; ?>").val(),scauth:'<?php echo $auth_code; ?>'});     
$.post
(
    '<?php echo PATH_TO_PROCESSING_SCRIPT; ?>?r=<?php echo $rand; ?>&nocache=' + Math.floor(Math.random()*1000), json_data,
    function(data)
    {
        // place the returned data in the results div
        $("#results<?php echo $rand; ?>").html(data);
        // div#response is returned by send.php. if the submission was successful, div#response will have the class ui-state-highlight. otherwise, the submission failed. Hide the form on success.
        if($("#response").hasClass('ui-state-highlight'))
        {
            $('#c-form<?php echo $rand; ?>').hide('slow');
        }
    }
); //close $.post

Here is the method that appends the File object onto a the javascript object.

function update_json_(json_data)
{
    if(has_element("input[type=file]"))
    {
        var inputs = $("input[type=file]");
        fileobjs = [];
        for (var i = 0; i < inputs.length; i++)
        {
            fileobjs.push(inputs.eq(i).prop("files")[0]);
        }
        $.extend(json_data, { files : fileobjs } );
    }
    return json_data;
}

But at the moment, I'm getting an error in firebug.

NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object
[Break On This Error]   

...catch(c){return _.u}},stringify:function(f){function g(c){return h.call(this,c,a...
Was it helpful?

Solution 2

I decided on just using the $.ajaxForm() library from malsup

OTHER TIPS

You need to use form data. . .

function update_json_(json_data)
{
    if(has_element("input[type=file]"))
    {
        var inputs = $("input[type=file]");
        fileobjs = [];
        for (var i = 0; i < inputs.length; i++)
        {
            fileobjs.push(inputs.eq(i).prop("files")[0]);
        }
        $.extend(json_data, { files : fileobjs } );
    }

    //add your files to form data. . 
    var fd = new FormData();    
    fd.append( 'files', fileobject );

    //similarly add your other data using fd.put('key', 'val');
    .
    . 

    //return data to be sent
    return fd;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top