2
Look at that:
HTML
<form id="form" enctype="multipart/form-data" action="paginax.php" method="post">
<input name="nome_arquivo" type="file"/>
<input type="hidden" value="valor_name1" name="name1"/>
<input type="hidden" value="valor_name" name="name2"/>
</form>
JAVASCRIPT
$(document).ready(function(){
$("#enviar_form").click(function(){
$.ajax({
method:"POST",
data: $("#form").serialize(),
url: "paginax.php",
contentType:"multipart/form-data; boundary=---------------------------1922310697355",
headers:{
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
"Content-Length":"781659",
"Content-Type":"multipart/form-data; boundary=---------------------------1922310697355",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0"
},
success:function(){alert("foi!");},
error: function(e){alert(e);}
});
});
});
The question is, I configured in ajax the header of the request(headers), and in the Content-type of the header I placed: multipart/form-data; boundary=---------------------------1922310697355
.
My expectation was that the data to be sent would be divided by what I determined in Boundary(---------------------------1922310697355).
However, when consulting the request that is made, I see it being put in this way:
name1=valor_name1&name2=valor_name
That is, in addition to not being divided as determined in Boundary, the image I selected in the form (in input[type=file]) seems not to be sent either.
I have three questions:
1° why is not sent the POST as I determined in the division?
2° why the image is not sent, since I do not see it in the data sent in the POST (unless it is available visually only by the magic var $_FILES)..
3°
if I determine in headers:
of $.jquery
the Content-type:...
, i need to configure the field/attribute Content:Type:
of $.jquery
of my request again?
Wallace, perfect answer, just one question left here, could you explain to me what this line does?
formData.append('image', $('#image').prop('files')[0])
– Ale
That means you’re taking the file from
input
of the kindfile
. In the case, as we’re theoretically talking about a 1 file input, we’re taking the index0
. This is the way to upload a file via ajax :)– Wallace Maxters
is it possible to say that attr() and prop() take the value of an attribute, but what attr() does not do is take the complex value, as in a file(file)? I researched about it and wondered at the size of the answer, rsrs.. http://stackoverflow.com/questions/5874652/prop-vs-attr
– Ale
In fact, I believe prop refers to the specific property of the object. For example, you can write an attribute with
attr
, however, withprop
you take the property of the element as an object in the DOM. For example, Body has thestyle
which is defined as an attribute, butinnerHTML
can be caught withprop
, that it is the property of that element (javascript) according to its type in the DOM. For example, with theprop
you can take thescrollHeight
, already with theattr
nay!– Wallace Maxters
Perfect Wallace, a big hug and thank you for collaborating!
– Ale