antHak antHak - 5 months ago 8
Ajax Question

I'm sending an Ajax request but I'm getting no response from my .php file?

I just want to state that Ajax and JQuery are really vague in my head so I'm still learning. However, I understand a little the idea behind just not the functionalities in details.

My example here is that I have this input type file :

<input id="photo" type="file" capture="camera" accept="image/*">


And I'm trying to simply send the file through Ajax request. Here's my code:

$(document).ready(function() {
$("#photo").change(function() {
var file = $("#photo").prop('files')[0];
var data = new FormData();
data.append(file.name, file);
$.ajax({
type: 'POST',
url: 'php.php',
processData: false,
contentType: false,
data: data,
success: function() {
alert("It was a success");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
}
});
});

});


Everything seems to work, the Ajax's response status return 200 but I don't feel like it's interacting with my php.

Here's my php:

<?php
echo $_FILES['file']['type'];
echo "COME ON WORK";
?>


Is it possible that it has something to do with Same Origin Policy? I looked around and found similar questions related to it.

Answer

If you are referring to the echos, you need to put resp in your success

   success: function(resp) {
                alert("It was a success");
                alert("PHP echos:" + resp);
            },
Comments